我正在尝试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()
似乎不起作用。