Here I create an invaluable expression:
e2 <- expression(x+10)
If I put an environment in which x is defined as
env <- as.environment(list(x=20)) eval(e2,env)
R will report an error:
Error in eval(expr, envir, enclos) : could not find function "+"
This is understandable, since env is an environment created from scratch, that is, it does not have a parent environment where + defined.
However, if I put + in a list that will be converted to an environment like this
env <- as.environment(list(x=20,`+`=function(a,b) {a+b})) eval(e2,env)
The rating works correctly and gives 30.
However, when I define + in the list, this is a binary function whose body also uses +, which is defined in {base} . I know that returning a function is lazily evaluated in R, but why might this work? If a+b in the function body is evaluated lazily when I call eval for e2 inside env , although + is defined in this environment, which does not have a parent environment, it should still call + by itself, which should end in an infinite loop. Why is this not happening? What is the mechanism here?
source share