Skip to content

Commit

Permalink
Fix mutate_each() and summarise_each() deprecation warnings (tidy…
Browse files Browse the repository at this point in the history
…verse#6884)

* Ensure that `mutate/summarise_each()` throw correct deprecation messages

* NEWS bullet
  • Loading branch information
DavisVaughan committed Jul 17, 2023
1 parent 3125697 commit c963d4d
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 9 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# dplyr (development version)

* `mutate_each()` and `summarise_each()` now throw correct deprecation messages
(#6869).

* `setequal()` now requires the input data frames to be compatible, similar to
the other set methods like `setdiff()` or `intersect()` (#6786).

Expand Down
51 changes: 42 additions & 9 deletions R/deprec-lazyeval.R
Original file line number Diff line number Diff line change
Expand Up @@ -359,17 +359,34 @@ summarize_ <- summarise_
#' @keywords internal
#' @export
summarise_each <- function(tbl, funs, ...) {
summarise_each_(tbl, funs, enquos(...))
summarise_each_impl(tbl, funs, enquos(...), "summarise_each")
}
#' @export
#' @rdname summarise_each
summarise_each_ <- function(tbl, funs, vars) {
lifecycle::deprecate_warn("0.7.0", "summarise_each_()", "across()", always = TRUE)
summarise_each_impl(tbl, funs, vars, "summarise_each_")
}
summarise_each_impl <- function(tbl,
funs,
vars,
name,
env = caller_env(),
user_env = caller_env(2)) {
what <- paste0(name, "()")

lifecycle::deprecate_warn(
when = "0.7.0",
what = what,
with = "across()",
always = TRUE,
env = env,
user_env = user_env
)

if (is_empty(vars)) {
vars <- tbl_nongroup_vars(tbl)
} else {
vars <- compat_lazy_dots(vars, caller_env())
vars <- compat_lazy_dots(vars, user_env)
vars <- tidyselect::vars_select(tbl_nongroup_vars(tbl), !!!vars)
if (length(vars) == 1 && names(vars) == as_string(vars)) {
vars <- unname(vars)
Expand All @@ -378,7 +395,7 @@ summarise_each_ <- function(tbl, funs, vars) {
if (is_character(funs)) {
funs <- funs_(funs)
}
funs <- manip_at(tbl, vars, funs, enquo(funs), caller_env(), .caller = "summarise_each_")
funs <- manip_at(tbl, vars, funs, enquo(funs), user_env, .caller = name)
summarise(tbl, !!!funs)
}

Expand All @@ -388,24 +405,40 @@ mutate_each <- function(tbl, funs, ...) {
if (is_character(funs)) {
funs <- funs_(funs)
}

mutate_each_(tbl, funs, enquos(...))
mutate_each_impl(tbl, funs, enquos(...), "mutate_each")
}
#' @export
#' @rdname summarise_each
mutate_each_ <- function(tbl, funs, vars) {
lifecycle::deprecate_warn("0.7.0", "mutate_each_()", "across()", always = TRUE)
mutate_each_impl(tbl, funs, vars, "mutate_each_")
}
mutate_each_impl <- function(tbl,
funs,
vars,
name,
env = caller_env(),
user_env = caller_env(2)) {
what <- paste0(name, "()")

lifecycle::deprecate_warn(
when = "0.7.0",
what = what,
with = "across()",
always = TRUE,
env = env,
user_env = user_env
)

if (is_empty(vars)) {
vars <- tbl_nongroup_vars(tbl)
} else {
vars <- compat_lazy_dots(vars, caller_env())
vars <- compat_lazy_dots(vars, user_env)
vars <- tidyselect::vars_select(tbl_nongroup_vars(tbl), !!!vars)
if (length(vars) == 1 && names(vars) == as_string(vars)) {
vars <- unname(vars)
}
}
funs <- manip_at(tbl, vars, funs, enquo(funs), caller_env(), .caller = "mutate_each_")
funs <- manip_at(tbl, vars, funs, enquo(funs), user_env, .caller = name)
mutate(tbl, !!!funs)
}

Expand Down
58 changes: 58 additions & 0 deletions tests/testthat/_snaps/deprec-lazyeval.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# mutate_each() and mutate_each_() are deprecated (#6869)

Code
mutate_each(df, list(~ .x + 1L))
Condition
Warning:
`mutate_each()` was deprecated in dplyr 0.7.0.
i Please use `across()` instead.
Output
# A tibble: 2 x 2
x y
<int> <int>
1 2 4
2 3 5

---

Code
mutate_each_(df, list(~ .x + 1L), c("x", "y"))
Condition
Warning:
`mutate_each_()` was deprecated in dplyr 0.7.0.
i Please use `across()` instead.
Output
# A tibble: 2 x 2
x y
<int> <int>
1 2 4
2 3 5

# summarise_each() and summarise_each_() are deprecated (#6869)

Code
summarise_each(df, list(mean))
Condition
Warning:
`summarise_each()` was deprecated in dplyr 0.7.0.
i Please use `across()` instead.
Output
# A tibble: 1 x 2
x y
<dbl> <dbl>
1 1.5 3.5

---

Code
summarise_each_(df, list(mean), c("x", "y"))
Condition
Warning:
`summarise_each_()` was deprecated in dplyr 0.7.0.
i Please use `across()` instead.
Output
# A tibble: 1 x 2
x y
<dbl> <dbl>
1 1.5 3.5

22 changes: 22 additions & 0 deletions tests/testthat/test-deprec-lazyeval.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,28 @@ test_that("mutate_each_() and summarise_each_() handle lazydots", {
expect_equal(cyl_mean, mean(mtcars$cyl))
})

test_that("mutate_each() and mutate_each_() are deprecated (#6869)", {
df <- tibble(x = 1:2, y = 3:4)

expect_snapshot({
mutate_each(df, list(~ .x + 1L))
})
expect_snapshot({
mutate_each_(df, list(~ .x + 1L), c("x", "y"))
})
})

test_that("summarise_each() and summarise_each_() are deprecated (#6869)", {
df <- tibble(x = 1:2, y = 3:4)

expect_snapshot({
summarise_each(df, list(mean))
})
expect_snapshot({
summarise_each_(df, list(mean), c("x", "y"))
})
})

test_that("select_vars_() handles lazydots", {
withr::local_options(lifecycle_verbosity = "quiet")

Expand Down

0 comments on commit c963d4d

Please sign in to comment.