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
44 changes: 28 additions & 16 deletions R/count-tally.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -230,24 +235,31 @@ 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 {
expr(sum(!!wt, na.rm = TRUE))
}
}

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,
Expand Down
68 changes: 68 additions & 0 deletions tests/testthat/_snaps/count-tally.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<int> <int>
1 1 1
2 2 1
3 3 1
4 4 1
5 5 1

# tally() owns errors (#6139)

Code
Expand All @@ -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
<int>
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
<int> <int>
1 1 1
2 2 1
3 3 1
4 4 1
5 5 1

# add_count() owns errors (#6139)

Code
Expand Down Expand Up @@ -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
<int> <int>
1 1 5
2 2 5
3 3 5
4 4 5
5 5 5

37 changes: 32 additions & 5 deletions tests/testthat/test-count-tally.R
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,21 @@ 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 + "")))
(expect_error(count(mtcars, wt = 1 + "")))
})
})

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", {
Expand All @@ -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", {
Expand All @@ -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 + "")))
Expand Down Expand Up @@ -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())
})
})