2
让我们的环境评估的表达式:在执行`eval`时,R在哪里查找对象?
> myenv <- new.env()
> assign("x", 2, myenv)
> f <- function(x) x+1
> eval(expression(f(x)), myenv)
[1] 3
我不明白为什么它的工作原理,因为f
不myenv
。 R如何找到f
?
让我们看看帮助:
Usage
eval(expr, envir = parent.frame(),
enclos = if(is.list(envir) || is.pairlist(envir))
parent.frame() else baseenv())
Arguments
envir
the environment in which expr is to be evaluated. May also be NULL, a list, a data frame, a pairlist or an integer as specified to sys.call.
enclos
Relevant when envir is a (pair)list or a data frame. Specifies the enclosure, i.e., where R looks for objects not found in envir. This can be NULL (interpreted as the base package environment, baseenv()) or an environment.
所以说,R还看起来enclos
,这是baseenv()
这里。但f
不在baseenv()
。
好的,谢谢。我发现文档不清楚。但是,也许我是那个误解的人...... –
当你的'envir ='参数不是一个可以有父环境的环境时 - 例如当'envir ='是一个列表时,使用'enclos'会更有意义或一个data.frame。 – MrFlick