【发布时间】:2012-09-11 14:46:00
【问题描述】:
我试图在一系列函数调用中使用get,但对象名称的查找似乎跳过了环境。例如:
foo <- 1 # variable in .GlobalEnv
getter <- function(x) {get(x)}
getter("foo") # returns 1, which is expected
f1 <- function() {
foo <- 2 # local variable in the function scope
getter("foo")
}
f1() # still returns 1, would've expected to return 2
为什么调用f1会返回全局环境中的foo,而不是调用函数环境中的foo?
如何让get 查看调用函数的环境?设置pos = sys.parent() 似乎不起作用。
【问题讨论】:
标签: r scope environment-variables