Uses the eval_tidy() function to evaluate an expression
within the context of the data
and env
arguments.
It provides two additional elements of flexibility:
data
does not need to be named, if it isn't named, it can be
references by .
or .data
.
expr
can be a function (preferably a fseq
object)
that will be applied to data
. What's more is that expr
will be evaluated through eval_tidy()
before being applied
and so functions defined within data
can also be used.
Note the data
argument is first (whereas in eval_tidy()
, it is
second) to keep in line with the pipeline theme of mpipe
.
eval_expr(data, expr, ..., env = NULL, allow_NULL = F, verbose = F)
data | data within which to evaluate. Used as a data mask in |
---|---|
expr | a call, function or expression to be evaluated within the context
of |
... | additional arguments to be attached to |
env | the environment in which to evaluate the expression |
allow_NULL | logical. Can |
verbose | logical. Should |
# Can do simple evaluations of functions on vectors 1:10 %>% eval_expr(mean)#> [1] 5.5# Or on a variable x <- 1:10 eval_expr(x, mean)#> [1] 5.5#> [1] 5.5#> [1] 5.5# functions are applied to data eval_expr(tbl, nrow)#> [1] 10#> [1] 5.5#> [1] 10.5#> [1] 10.5# Environment scope: lst <- list(x = 1:3, y = 1) y <- 4 e1 <- new.env(parent = emptyenv()) e1$y <- 3 # additional arguments in data take priority over others eval_expr(lst, y, y = 2, env = e1)#> [1] 1lst$y <- NULL # then the ... argument: eval_expr(lst, y, y = 2, env = e1)#> [1] 2# then in env eval_expr(lst, y, env = e1)#> [1] 3# without env, the calling environment is used eval_expr(lst, y)#> [1] 4