Skip to content

Commit 0bc71b3

Browse files
committed
Ensure that compute_by() is type stable on $data
It should always return a bare tibble, even though `group_data()` returns a data frame for data frame input.
1 parent 63643b7 commit 0bc71b3

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

R/by.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ compute_by <- function(by,
5454
type <- "ungrouped"
5555
names <- by
5656
data <- group_data(data)
57+
data <- as_tibble(data)
5758
} else {
5859
type <- "grouped"
5960
names <- by

tests/testthat/test-by.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ test_that("computes `by` group data in order of appearance", {
2929
})
3030

3131
test_that("extracts existing data when `by = NULL`", {
32+
df <- data.frame(x = c(1, 1, 2, 2, 1))
33+
out <- compute_by(by = NULL, data = df)
34+
expect_identical(out$type, "ungrouped")
35+
expect_identical(out$names, character())
36+
# `compute_by()` is always type stable on `$data` and returns a bare tibble
37+
expect_identical(out$data, as_tibble(group_data(df)))
38+
3239
df <- tibble(x = c(1, 1, 2, 2, 1))
3340
out <- compute_by(by = NULL, data = df)
3441
expect_identical(out$type, "ungrouped")

0 commit comments

Comments
 (0)