-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #154
- Loading branch information
Showing
15 changed files
with
303 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ Imports: | |
crayon, | ||
data.table (>= 1.12.4), | ||
dplyr (>= 1.0.0), | ||
glue, | ||
lifecycle, | ||
rlang, | ||
tibble, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
capture_across <- function(vars, x, j = TRUE) { | ||
x <- enquo(x) | ||
dt_squash_across(get_expr(x), get_env(x), vars, j) | ||
} | ||
|
||
dt_squash_across <- function(call, env, vars, j = j) { | ||
call <- match.call(dplyr::across, call, expand.dots = FALSE, envir = env) | ||
|
||
tbl <- as_tibble(rep_named(vars, list(logical()))) | ||
locs <- tidyselect::eval_select(call$.cols, tbl, allow_rename = FALSE) | ||
cols <- syms(vars)[locs] | ||
|
||
funs <- across_funs(call$.fns, env, vars, j = j) | ||
|
||
# Generate grid of expressions | ||
out <- vector("list", length(cols) * length(funs)) | ||
k <- 1 | ||
for (i in seq_along(cols)) { | ||
for (j in seq_along(funs)) { | ||
out[[k]] <- exec(funs[[j]], cols[[i]], !!!call$...) | ||
k <- k + 1 | ||
} | ||
} | ||
|
||
.names <- eval(call$.names, env) | ||
if (!is.null(call$.fns)) { | ||
names(out) <- across_names(vars[locs], names(funs), .names, env) | ||
} | ||
out | ||
} | ||
|
||
across_funs <- function(funs, env, vars, j = TRUE) { | ||
if (is.null(funs)) { | ||
list(function(x, ...) x) | ||
} else if (is_symbol(funs)) { | ||
set_names(list(across_fun(funs, env, vars, j = j)), as.character(funs)) | ||
} else if (is.character(funs)) { | ||
names(funs)[names2(funs) == ""] <- funs | ||
lapply(funs, across_fun, env, vars, j = j) | ||
} else if (is_call(funs, "~")) { | ||
set_names(list(across_fun(funs, env, vars, j = j)), expr_name(f_rhs(funs))) | ||
} else if (is_call(funs, "list")) { | ||
args <- rlang::exprs_auto_name(funs[-1]) | ||
lapply(args, across_fun, env, vars, j = j) | ||
} else if (!is.null(env)) { | ||
# Try evaluating once, just in case | ||
funs <- eval(funs, env) | ||
across_funs(funs, NULL) | ||
} else { | ||
abort("`.fns` argument to dtplyr::across() must be a NULL, a function name, formula, or list") | ||
} | ||
} | ||
|
||
across_fun <- function(fun, env, vars, j = TRUE) { | ||
if (is_symbol(fun) || is_string(fun)) { | ||
function(x, ...) call2(fun, x, ...) | ||
} else if (is_call(fun, "~")) { | ||
call <- f_rhs(fun) | ||
call <- replace_dot(call, quote(!!.x)) | ||
call <- dt_squash_call(call, env, vars, j = TRUE) | ||
|
||
function(x, ...) expr_interp(call, child_env(emptyenv(), .x = x)) | ||
} else { | ||
abort(c( | ||
".fns argument to dtplyr::across() contain a function name or a formula", | ||
x = paste0("Problem with ", expr_deparse(fun)) | ||
)) | ||
} | ||
} | ||
|
||
across_names <- function(cols, funs, names = NULL, env = parent.frame()) { | ||
if (length(funs) == 1) { | ||
names <- names %||% "{.col}" | ||
} else { | ||
names <- names %||% "{.col}_{.fn}" | ||
} | ||
|
||
glue_env <- child_env(env, | ||
.col = rep(cols, each = length(funs)), | ||
.fn = rep(funs %||% seq_along(funs), length(cols)) | ||
) | ||
glue::glue(names, .envir = glue_env) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# has useful display methods | ||
|
||
Code | ||
dt <- lazy_dt(mtcars, "DT") | ||
Code | ||
dt | ||
Output | ||
Source: local data table [32 x 11] | ||
Call: DT | ||
mpg cyl disp hp drat wt qsec vs am gear carb | ||
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> | ||
1 21 6 160 110 3.9 2.62 16.5 0 1 4 4 | ||
2 21 6 160 110 3.9 2.88 17.0 0 1 4 4 | ||
3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1 | ||
4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1 | ||
5 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2 | ||
6 18.1 6 225 105 2.76 3.46 20.2 1 0 3 1 | ||
# Use as.data.table()/as.data.frame()/as_tibble() to access results | ||
Code | ||
dt %>% group_by(vs, am) | ||
Output | ||
Source: local data table [?? x 11] | ||
Call: DT | ||
Groups: vs, am | ||
mpg cyl disp hp drat wt qsec vs am gear carb | ||
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> | ||
1 21 6 160 110 3.9 2.62 16.5 0 1 4 4 | ||
2 21 6 160 110 3.9 2.88 17.0 0 1 4 4 | ||
3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1 | ||
4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1 | ||
5 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2 | ||
6 18.1 6 225 105 2.76 3.46 20.2 1 0 3 1 | ||
# Use as.data.table()/as.data.frame()/as_tibble() to access results | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# across() gives informative errors | ||
|
||
Code | ||
capture_across(letters, across(a, 1)) | ||
Error <rlang_error> | ||
`.fns` argument to dtplyr::across() must be a NULL, a function name, formula, or list | ||
Code | ||
capture_across(letters, across(a, list(1))) | ||
Error <rlang_error> | ||
.fns argument to dtplyr::across() contain a function name or a formula | ||
x Problem with 1 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.