eval_expr: Evaluates an expression within a data context

Description Usage Arguments Examples

View source: R/eval_expr.R

Description

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:

Note the data argument is first (whereas in eval_tidy(), it is second) to keep in line with the pipeline theme of mpipe.

Usage

1
eval_expr(data, expr, ..., env = NULL, allow_NULL = F, verbose = F)

Arguments

data

data within which to evaluate. Used as a data mask in eval_tidy()

expr

a call, function or expression to be evaluated within the context of data

...

additional arguments to be attached to data during evaluation.

env

the environment in which to evaluate the expression

allow_NULL

logical. Can eval_expr() return a NULL value? By default, NULLs will result in an error.

verbose

logical. Should eval_expr() be chatty?

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Can do simple evaluations of functions on vectors
1:10 %>% eval_expr(mean)

# Or on a variable
x <- 1:10
eval_expr(x, mean)

# or within a data.frame (or tibble)
tbl <- data.frame(x = 1:10)
eval_expr(tbl, mean(x))

# or a list
lst <- list(x = 1:10)
eval_expr(lst, mean(x))

# functions are applied to data
eval_expr(tbl, nrow)

# but they are evaluated within data first
lst <- c(fun = mean, lst)
eval_expr(lst, fun(x))

# additional named arguments can be passed in too
tbl %>%
  eval_expr(mean(x + y), y = 5)

x %>%
  eval_expr(. %>% magrittr::add(y) %>% mean(), y = 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)

lst$y <- NULL
# then the ... argument:
eval_expr(lst, y, y = 2, env = e1)

# then in env
eval_expr(lst, y, env = e1)

# without env, the calling environment is used
eval_expr(lst, y)

MyKo101/mpipe documentation built on Feb. 6, 2021, 2:13 p.m.