Skip to content

Commit

Permalink
Fix partial ungrouping (#6607)
Browse files Browse the repository at this point in the history
* Fix partial ungrouping

* Disallow renaming during partial ungrouping
  • Loading branch information
DavisVaughan authored Dec 14, 2022
1 parent 4132fa0 commit 3b696db
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
7 changes: 6 additions & 1 deletion R/group-by.R
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,12 @@ ungroup.grouped_df <- function(x, ...) {
as_tibble(x)
} else {
old_groups <- group_vars(x)
to_remove <- tidyselect::eval_select(expr(c(...)), x)
to_remove <- tidyselect::eval_select(
expr = expr(c(...)),
data = x,
allow_rename = FALSE
)
to_remove <- names(to_remove)

new_groups <- setdiff(old_groups, to_remove)
group_by(x, !!!syms(new_groups))
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/_snaps/group-by.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# can't rename while partially `ungroup()`-ing (#6606)

Code
ungroup(gdf, g2 = g)
Condition
Error in `ungroup()`:
! Can't rename variables in this context.

# select(group_by(.)) implicitely adds grouping variables (#170)

Code
Expand Down
17 changes: 17 additions & 0 deletions tests/testthat/test-group-by.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,23 @@ test_that("grouping by constant adds column (#410)", {
expect_equal(nrow(grouped), 1L)
})

test_that("can partially `ungroup()` (#6606)", {
df <- tibble(g1 = 1:2, g2 = 3:4, x = 5:6)
gdf <- group_by(df, g1, g2)

expect_identical(ungroup(gdf, g1), group_by(df, g2))
expect_identical(ungroup(gdf, g1, g2), df)
})

test_that("can't rename while partially `ungroup()`-ing (#6606)", {
df <- tibble(g = 1:2, x = 3:4)
gdf <- group_by(df, g)

expect_snapshot(error = TRUE, {
ungroup(gdf, g2 = g)
})
})

test_that(".dots is soft deprecated", {
rlang::local_options(lifecycle_verbosity = "warning")

Expand Down

0 comments on commit 3b696db

Please sign in to comment.