Skip to content

Commit

Permalink
across() and c_across() when used out of dplyr context (tidyverse#5194)
Browse files Browse the repository at this point in the history
* Better error messages for across() and c_across() when used out of dplyr context.

closes tidyverse#5180

* no need for fun =

* simplify condition
  • Loading branch information
romainfrancois committed May 6, 2020
1 parent 1ce7c96 commit bdef591
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions R/across.R
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ c_across <- function(cols = everything()) {
# to do any required "set up" work (like the `eval_select()` call) a single
# time per top-level call, rather than once per group.
across_setup <- function(cols, fns, names, key) {
mask <- peek_mask()
mask <- peek_mask("across()")

value <- mask$across_cache_get(key)
if (!is.null(value)) {
Expand Down Expand Up @@ -220,7 +220,7 @@ across_setup <- function(cols, fns, names, key) {
}

c_across_setup <- function(cols, key) {
mask <- peek_mask()
mask <- peek_mask("c_across()")

value <- mask$across_cache_get(key)
if (!is.null(value)) {
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test-across-errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,13 @@ Error: Problem with `summarise()` input `res`.
x `.fns` must be NULL, a function, a formula, or a list of functions/formulas
i Input `res` is `across(is.numeric, 42)`.


outside context
===============

> across()
Error: across() must only be used inside dplyr verbs

> c_across()
Error: c_across() must only be used inside dplyr verbs

4 changes: 4 additions & 0 deletions tests/testthat/test-across.R
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ test_that("across() gives meaningful messages", {
verify_output(test_path("test-across-errors.txt"), {
tibble(x = 1) %>%
summarise(res = across(is.numeric, 42))

"# outside context"
across()
c_across()
})
})

Expand Down

0 comments on commit bdef591

Please sign in to comment.