Skip to content

Commit

Permalink
Deprecate .add argument to group_by()
Browse files Browse the repository at this point in the history
Fixes #156
  • Loading branch information
hadley committed Jan 26, 2021
1 parent 3bba40b commit 7664bd2
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 6 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ Depends:
Imports:
crayon,
data.table (>= 1.12.4),
dplyr (>= 0.8.1),
dplyr (>= 1.0.0),
lifecycle,
rlang,
tibble,
tidyselect
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ importFrom(dplyr,transmute)
importFrom(dplyr,ungroup)
importFrom(dplyr,union)
importFrom(dplyr,union_all)
importFrom(lifecycle,deprecated)
importFrom(tibble,as_tibble)
importFrom(utils,head)
importFrom(utils,tail)
1 change: 1 addition & 0 deletions R/dtplyr-package.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#' @import rlang
#' @importFrom data.table data.table as.data.table .SD copy is.data.table
#' @importFrom lifecycle deprecated
"_PACKAGE"

#' @export
Expand Down
13 changes: 11 additions & 2 deletions R/step-group.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,25 @@ add_grouping_param <- function(call, step) {
#' @importFrom dplyr group_by
#' @rdname single_table
#' @export
group_by.dtplyr_step <- function(.data, ..., add = FALSE, arrange = TRUE) {
group_by.dtplyr_step <- function(.data, ..., .add = FALSE, add = deprecated(), arrange = TRUE) {
dots <- capture_dots(.data, ...)

if (lifecycle::is_present(add)) {
lifecycle::deprecate_warn(
"1.0.0",
"dplyr::group_by(add = )",
"group_by(.add = )"
)
.add <- add
}

existing <- vapply(dots, is_symbol, logical(1))
if (!all(existing)) {
.data <- mutate(.data, !!!dots[!existing])
dots[!existing] <- syms(names(dots[!existing]))
}

groups <- c(if (add) .data$groups, names(dots))
groups <- c(if (.add) .data$groups, names(dots))
arranged <- if (!is.null(.data$arrange)) .data$arrange && arrange else arrange

step_group(.data, groups, arranged)
Expand Down
10 changes: 9 additions & 1 deletion man/single_table.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions tests/testthat/_snaps/step-group.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# can add groups if requested

Code
. <- dt %>% group_by(x) %>% group_by(y, add = TRUE)
Warning <lifecycle_warning_deprecated>
The `add` argument of `group_by()` is deprecated as of dplyr 1.0.0.
Please use the `.add` argument instead.

7 changes: 5 additions & 2 deletions tests/testthat/test-step-group.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ test_that("grouping and ungrouping adjust groups field", {
expect_equal(dt %>% group_by(x) %>% ungroup() %>% .$groups, character())
})


test_that("can add groups if requested", {
dt <- lazy_dt(data.frame(x = 1:3, y = 1:3), "DT")
expect_equal(
dt %>% group_by(x) %>% group_by(y, add = TRUE) %>% .$groups,
dt %>% group_by(x) %>% group_by(y, .add = TRUE) %>% .$groups,
c("x", "y")
)

expect_snapshot({
. <- dt %>% group_by(x) %>% group_by(y, add = TRUE)
})
})

test_that("grouping can compute new variables if needed", {
Expand Down

0 comments on commit 7664bd2

Please sign in to comment.