Skip to content

Commit

Permalink
Test that summarize() strips off the subclass (#6988)
Browse files Browse the repository at this point in the history
* Test that `summarize()` strips off the subclass

* Align snapshot tests (#6987)

* Align snapshot tests

* Bump rlang and cli for snapshot tests

---------

Co-authored-by: Davis Vaughan <davis@posit.co>

* Tweak the test a little

* Mention `summarise()` / `reframe()` behavior in extension docs

---------

Co-authored-by: Davis Vaughan <davis@posit.co>
  • Loading branch information
krlmlr and DavisVaughan committed Feb 13, 2024
1 parent cf45725 commit 240da9e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
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.

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

0 comments on commit 240da9e

Please sign in to comment.