From f6a68614522b40e4a3092afbf22de51f577b2082 Mon Sep 17 00:00:00 2001 From: Davis Vaughan Date: Thu, 30 Oct 2025 14:11:07 -0400 Subject: [PATCH] Use lifecycle tooling to deprecate `wt = n()` and add tests --- R/count-tally.R | 44 +++++++++++------- tests/testthat/_snaps/count-tally.md | 68 ++++++++++++++++++++++++++++ tests/testthat/test-count-tally.R | 37 +++++++++++++-- 3 files changed, 128 insertions(+), 21 deletions(-) diff --git a/R/count-tally.R b/R/count-tally.R index d2a7c02206..0978eb9a98 100644 --- a/R/count-tally.R +++ b/R/count-tally.R @@ -102,7 +102,8 @@ count.data.frame <- function( out <- x } - out <- tally(out, wt = !!enquo(wt), sort = sort, name = name) + wt <- compat_wt(enquo(wt)) + out <- tally(out, wt = !!wt, sort = sort, name = name) # Ensure grouping is transient out <- dplyr_reconstruct(out, x) @@ -122,7 +123,8 @@ tally.data.frame <- function(x, wt = NULL, sort = FALSE, name = NULL) { dplyr_local_error_call() - n <- tally_n(x, {{ wt }}) + wt <- compat_wt(enquo(wt)) + n <- tally_n(x, wt) local_options(dplyr.summarise.inform = FALSE) out <- summarise(x, !!name := !!n) @@ -193,7 +195,8 @@ add_count_impl <- function( sort = FALSE, name = NULL, .drop = deprecated(), - error_call = caller_env() + error_call = caller_env(), + user_env = caller_env(2) ) { if (!is_missing(.drop)) { lifecycle::deprecate_warn("1.0.0", "add_count(.drop = )", always = TRUE) @@ -207,7 +210,8 @@ add_count_impl <- function( out <- x } - add_tally(out, wt = {{ wt }}, sort = sort, name = name) + wt <- compat_wt(enquo(wt), env = error_call, user_env = user_env) + add_tally(out, wt = !!wt, sort = sort, name = name) } #' @rdname count @@ -217,7 +221,8 @@ add_tally <- function(x, wt = NULL, sort = FALSE, name = NULL) { dplyr_local_error_call() - n <- tally_n(x, {{ wt }}) + wt <- compat_wt(enquo(wt)) + n <- tally_n(x, wt) out <- mutate(x, !!name := !!n) if (sort) { @@ -230,17 +235,6 @@ add_tally <- function(x, wt = NULL, sort = FALSE, name = NULL) { # Helpers ----------------------------------------------------------------- tally_n <- function(x, wt) { - wt <- enquo(wt) - - if (is_call(quo_get_expr(wt), "n", n = 0)) { - # Provided only by dplyr 1.0.0. See #5349 for discussion. - warn(c( - "`wt = n()` is deprecated", - i = "You can now omit the `wt` argument" - )) - wt <- quo(NULL) - } - if (quo_is_null(wt)) { expr(n()) } else { @@ -248,6 +242,24 @@ tally_n <- function(x, wt) { } } +compat_wt <- function(wt, env = caller_env(), user_env = caller_env(2)) { + if (!is_call(quo_get_expr(wt), "n", n = 0)) { + return(wt) + } + + # Provided only by dplyr 1.0.0. See #5349 for discussion. + lifecycle::deprecate_warn( + when = "1.0.1", + what = I("`wt = n()`"), + details = "You can now omit the `wt` argument.", + env = env, + user_env = user_env, + always = TRUE + ) + + quo(NULL) +} + check_n_name <- function( name, vars, diff --git a/tests/testthat/_snaps/count-tally.md b/tests/testthat/_snaps/count-tally.md index 7e4e89d729..11ca2d4459 100644 --- a/tests/testthat/_snaps/count-tally.md +++ b/tests/testthat/_snaps/count-tally.md @@ -59,6 +59,24 @@ Caused by error in `1 + ""`: ! non-numeric argument to binary operator +# count() `wt = n()` is deprecated + + Code + count(df, a, wt = n()) + Condition + Warning: + `wt = n()` was deprecated in dplyr 1.0.1. + i You can now omit the `wt` argument. + Output + # A tibble: 5 x 2 + a n + + 1 1 1 + 2 2 1 + 3 3 1 + 4 4 1 + 5 5 1 + # tally() owns errors (#6139) Code @@ -70,6 +88,38 @@ Caused by error in `1 + ""`: ! non-numeric argument to binary operator +# tally() `wt = n()` is deprecated + + Code + tally(df, wt = n()) + Condition + Warning: + `wt = n()` was deprecated in dplyr 1.0.1. + i You can now omit the `wt` argument. + Output + # A tibble: 1 x 1 + n + + 1 5 + +# add_count() `wt = n()` is deprecated + + Code + add_count(df, a, wt = n()) + Condition + Warning: + `wt = n()` was deprecated in dplyr 1.0.1. + i You can now omit the `wt` argument. + Output + # A tibble: 5 x 2 + a n + + 1 1 1 + 2 2 1 + 3 3 1 + 4 4 1 + 5 5 1 + # add_count() owns errors (#6139) Code @@ -100,3 +150,21 @@ Caused by error in `1 + ""`: ! non-numeric argument to binary operator +# add_tally() `wt = n()` is deprecated + + Code + add_tally(df, wt = n()) + Condition + Warning: + `wt = n()` was deprecated in dplyr 1.0.1. + i You can now omit the `wt` argument. + Output + # A tibble: 5 x 2 + a n + + 1 1 5 + 2 2 5 + 3 3 5 + 4 4 5 + 5 5 5 + diff --git a/tests/testthat/test-count-tally.R b/tests/testthat/test-count-tally.R index 547157364c..374175f7bf 100644 --- a/tests/testthat/test-count-tally.R +++ b/tests/testthat/test-count-tally.R @@ -123,11 +123,6 @@ test_that("can only explicitly chain together multiple tallies", { }) }) -test_that("wt = n() is deprecated", { - df <- data.frame(x = 1:3) - expect_warning(count(df, wt = n()), "`wt = n()`", fixed = TRUE) -}) - test_that("count() owns errors (#6139)", { expect_snapshot({ (expect_error(count(mtcars, new = 1 + ""))) @@ -135,6 +130,14 @@ test_that("count() owns errors (#6139)", { }) }) +test_that("count() `wt = n()` is deprecated", { + df <- tibble(a = 1:5) + + expect_snapshot({ + count(df, a, wt = n()) + }) +}) + # tally ------------------------------------------------------------------- test_that("tally can sort output", { @@ -161,6 +164,14 @@ test_that("tally() owns errors (#6139)", { }) }) +test_that("tally() `wt = n()` is deprecated", { + df <- tibble(a = 1:5) + + expect_snapshot({ + tally(df, wt = n()) + }) +}) + # add_count --------------------------------------------------------------- test_that("add_count preserves grouping", { @@ -178,6 +189,14 @@ test_that(".drop is deprecated", { expect_warning(out <- add_count(df, f, .drop = FALSE), "deprecated") }) +test_that("add_count() `wt = n()` is deprecated", { + df <- tibble(a = 1:5) + + expect_snapshot({ + add_count(df, a, wt = n()) + }) +}) + test_that("add_count() owns errors (#6139)", { expect_snapshot({ (expect_error(add_count(mtcars, new = 1 + ""))) @@ -215,3 +234,11 @@ test_that("add_tally() owns errors (#6139)", { (expect_error(add_tally(mtcars, wt = 1 + ""))) }) }) + +test_that("add_tally() `wt = n()` is deprecated", { + df <- tibble(a = 1:5) + + expect_snapshot({ + add_tally(df, wt = n()) + }) +})