# expr_eval() works just like base::eval():
env <- new_env(data = list(foo = "bar"))
expr <- quote(foo)
expr_eval(expr, env)
# To explore the consequences of stack inconsistent semantics, let's
# create a function that evaluates `parent.frame()` deep in the call
# stack, in an environment corresponding to a frame in the middle of
# the stack. For consistency we R's lazy evaluation semantics, we'd
# expect to get the caller of that frame as result:
fn <- function(eval_fn) {
list(
returned_env = middle(eval_fn),
actual_env = env()
)
}
middle <- function(eval_fn) {
deep(eval_fn, env())
}
deep <- function(eval_fn, eval_env) {
expr <- quote(parent.frame())
eval_fn(expr, eval_env)
}
# With expr_eval(), we do get the expected environment:
fn(rlang::expr_eval)
# But that's not the case with base::eval():
fn(base::eval)
# Another difference of expr_eval() compared to base::eval() is
# that it does not insert parasite frames in the evaluation stack:
get_stack <- quote(identity(eval_stack()))
expr_eval(get_stack)
eval(get_stack)
Run the code above in your browser using DataLab