Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Across evaluated lists #817

Merged
merged 4 commits into from
Apr 11, 2022
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
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# dbplyr (development version)

* `across()` now works if passing the name of a list of functions to the `.fns`
argument (@mgirlich, #817).

* Added translations for lubridate functions `day()`, `week()`, `isoweek()`,
and `isoyear()` for Postgres (@mgirlich, #675).

Expand Down
16 changes: 16 additions & 0 deletions R/tidyeval-across.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,24 @@ across_funs <- function(funs, env, data, dots, names_spec, fn, evaluated = FALSE
if (is.null(funs)) {
fns <- list(`1` = function(x, ...) x)
names_spec <- names_spec %||% "{.col}"
} else if (is_quosure(funs)) {
return(across_funs(quo_squash(funs), env, data, dots, names_spec, fn, evaluated = evaluated))
} else if (is_symbol(funs) || is_function(funs) ||
is_call(funs, "~") || is_call(funs, "function")) {
is_local_list <- function(funs) {
if (!is_symbol(funs)) {
return(FALSE)
}

funs <- as_name(funs)
exists(funs, env) && is.list(get(funs, envir = env))
}

if (is_local_list(funs)) {
funs <- eval(funs, env)
return(across_funs(funs, env, data, dots, names_spec, fn, evaluated = evaluated))
}

fns <- list(`1` = across_fun(funs, env, data, dots = dots, fn = fn))
names_spec <- names_spec %||% "{.col}"
} else if (is_call(funs, "list")) {
Expand Down
44 changes: 44 additions & 0 deletions tests/testthat/test-tidyeval-across.R
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,50 @@ test_that("across() translates evaluated lists", {
)
})

test_that("across() translates evaluated quosures", {
lf <- lazy_frame(a = 1, b = 2)

expect_equal(
capture_across(lf, across(a:b, !!quo(list(log)))),
exprs(
a_1 = log(a),
b_1 = log(b)
)
)

expect_equal(
capture_across(lf, across(a:b, !!quo(~ log(.x, 2)))),
exprs(
a = log(a, 2),
b = log(b, 2)
)
)

expect_equal(
capture_across(lf, across(a:b, !!quo(list(log2 = ~ log(.x, 2))))),
exprs(
a_log2 = log(a, 2),
b_log2 = log(b, 2)
)
)
})

test_that("across() searches for list in environment", {
lf <- lazy_frame(a = 1, b = 2)
list_formula <- list(~ log(.x), sum)

expect_equal(
capture_across(lf, across(a:b, list_formula)),
exprs(
a_1 = log(a),
a_2 = sum(a),
b_1 = log(b),
b_2 = sum(b)
)
)
})


# if_all ------------------------------------------------------------------

test_that("if_all() translates functions", {
Expand Down