Skip to content
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
11 changes: 2 additions & 9 deletions R/group-by.r
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,8 @@ group_by_prepare <- function(.data, ..., .add = FALSE, .dots = deprecated(), add
}

# If any calls, use mutate to add new columns, then group by those
ungrouped <- ungroup(.data)
atts <- attributes(.data)
atts <- append(
attributes(ungrouped),
atts[setdiff(names(atts), c("names", "row.names", "groups", "class"))]
)
attributes(ungrouped) <- atts
computed_columns <- add_computed_columns(.data, new_groups, "group_by")

computed_columns <- add_computed_columns(ungrouped, new_groups, "group_by")
out <- computed_columns$data
group_names <- computed_columns$added_names

Expand Down Expand Up @@ -188,7 +181,7 @@ add_computed_columns <- function(.data, vars, .fn = "group_by") {
# TODO: use less of a hack
if (inherits(.data, "data.frame")) {
cols <- withCallingHandlers(
mutate_cols(.data, !!!vars),
mutate_cols(ungroup(.data), !!!vars),
error = function(e) {
abort(c(
glue("Problem adding computed columns in `{.fn}()`."),
Expand Down
3 changes: 3 additions & 0 deletions tests/testthat/test-group-by.r
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,9 @@ test_that("group_by() keeps attributes unrelated to the grouping (#5760)", {

d3 <- group_by(d2, y, .add = TRUE)
expect_equal(attr(d2, "foo"), "bar")

d4 <- group_by(d2, y2 = y * 2, .add = TRUE)
expect_equal(attr(d2, "foo"), "bar")
})

# Errors ------------------------------------------------------------------
Expand Down