Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test that summarize() strips off the subclass #6988

Merged
merged 4 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ BugReports: https://github.com/tidyverse/dplyr/issues
Depends:
R (>= 3.5.0)
Imports:
cli (>= 3.4.0),
cli (>= 3.6.2),
generics,
glue (>= 1.3.2),
lifecycle (>= 1.0.3),
magrittr (>= 1.5),
methods,
pillar (>= 1.9.0),
R6,
rlang (>= 1.1.0),
rlang (>= 1.1.3),
tibble (>= 3.2.0),
tidyselect (>= 1.2.0),
utils,
Expand Down
4 changes: 3 additions & 1 deletion R/generics.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@
#'
#' * `summarise()` and `reframe()` work similarly to `mutate()` but the data
#' modified by `dplyr_col_modify()` comes from `group_data()` or is built
#' from `.by`.
#' from `.by`. Note that this means that the data frames returned by
#' `summarise()` and `reframe()` are fundamentally new data frames, and
#' will not retain any custom subclasses or attributes.
#'
#' * `select()` uses 1d `[` to select columns, then `names<-` to rename them.
#' `rename()` just uses `names<-`. `relocate()` just uses 1d `[`.
Expand Down
4 changes: 3 additions & 1 deletion man/dplyr_extending.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/testthat/_snaps/context.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@
Code
group_labels_details(c(a = 1, b = 2))
Output
[1] "`a = 1`, `b = 2`"
[1] "`a = 1` and `b = 2`"

2 changes: 1 addition & 1 deletion tests/testthat/_snaps/summarise.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
<error/rlang_error>
Error in `summarise()`:
i In argument: `a = rlang::env(a = 1)`.
i In group 1: `x = 1`, `y = 1`.
i In group 1: `x = 1` and `y = 1`.
Caused by error:
! `a` must be a vector, not an environment.
Code
Expand Down
29 changes: 26 additions & 3 deletions tests/testthat/test-summarise.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,44 @@ test_that("no expressions yields grouping data", {
expect_equal(summarise(gf, !!!list()), tibble(x = 1:2))
})

test_that("preserved class, but not attributes", {
test_that("doesn't preserve attributes", {
df <- structure(
data.frame(x = 1:10, g1 = rep(1:2, each = 5), g2 = rep(1:5, 2)),
meta = "this is important"
)

out <- df %>% summarise(n = n())
expect_s3_class(out, "data.frame", exact = TRUE)
expect_null(attr(out, "res"))

out <- df %>% group_by(g1) %>% summarise(n = n())
# expect_s3_class(out, "data.frame", exact = TRUE)
expect_null(attr(out, "res"))
})

test_that("strips off subclass", {
# We consider the data frame returned by `summarise()` to be
# "fundamentally a new data frame"

df <- new_data_frame(list(a = 1), class = "myclass")
out <- df %>% summarise(n = n())
expect_s3_class(out, "data.frame", exact = TRUE)
out <- df %>% summarise(.by = a, n = n())
expect_s3_class(out, "data.frame", exact = TRUE)

df <- new_tibble(list(a = 1), class = "myclass")
out <- df %>% summarise(n = n())
expect_s3_class(out, class(tibble()), exact = TRUE)
out <- df %>% summarise(.by = a, n = n())
expect_s3_class(out, class(tibble()), exact = TRUE)

gdf <- group_by(tibble(a = 1), a)
df <- gdf
class(df) <- c("myclass", class(gdf))
out <- df %>% summarise(n = n(), .groups = "drop")
expect_s3_class(out, class(tibble()), exact = TRUE)
out <- df %>% summarise(n = n(), .groups = "keep")
expect_s3_class(out, class(gdf), exact = TRUE)
})

test_that("works with unquoted values", {
df <- tibble(g = c(1, 1, 2, 2, 2), x = 1:5)
expect_equal(summarise(df, out = !!1), tibble(out = 1))
Expand Down
Loading