Skip to content

Commit 3b696db

Browse files
authored
Fix partial ungrouping (#6607)
* Fix partial ungrouping * Disallow renaming during partial ungrouping
1 parent 4132fa0 commit 3b696db

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

R/group-by.R

+6-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,12 @@ ungroup.grouped_df <- function(x, ...) {
159159
as_tibble(x)
160160
} else {
161161
old_groups <- group_vars(x)
162-
to_remove <- tidyselect::eval_select(expr(c(...)), x)
162+
to_remove <- tidyselect::eval_select(
163+
expr = expr(c(...)),
164+
data = x,
165+
allow_rename = FALSE
166+
)
167+
to_remove <- names(to_remove)
163168

164169
new_groups <- setdiff(old_groups, to_remove)
165170
group_by(x, !!!syms(new_groups))

tests/testthat/_snaps/group-by.md

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# can't rename while partially `ungroup()`-ing (#6606)
2+
3+
Code
4+
ungroup(gdf, g2 = g)
5+
Condition
6+
Error in `ungroup()`:
7+
! Can't rename variables in this context.
8+
19
# select(group_by(.)) implicitely adds grouping variables (#170)
210

311
Code

tests/testthat/test-group-by.R

+17
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,23 @@ test_that("grouping by constant adds column (#410)", {
7272
expect_equal(nrow(grouped), 1L)
7373
})
7474

75+
test_that("can partially `ungroup()` (#6606)", {
76+
df <- tibble(g1 = 1:2, g2 = 3:4, x = 5:6)
77+
gdf <- group_by(df, g1, g2)
78+
79+
expect_identical(ungroup(gdf, g1), group_by(df, g2))
80+
expect_identical(ungroup(gdf, g1, g2), df)
81+
})
82+
83+
test_that("can't rename while partially `ungroup()`-ing (#6606)", {
84+
df <- tibble(g = 1:2, x = 3:4)
85+
gdf <- group_by(df, g)
86+
87+
expect_snapshot(error = TRUE, {
88+
ungroup(gdf, g2 = g)
89+
})
90+
})
91+
7592
test_that(".dots is soft deprecated", {
7693
rlang::local_options(lifecycle_verbosity = "warning")
7794

0 commit comments

Comments
 (0)