【发布时间】:2021-07-27 23:03:00
【问题描述】:
喂! 我正在玩R中的环境。 这是一个最小的例子:
test_function <- function() {
list2env(list(test = 5), envir = sys.frame(sys.nframe()))
test_function2()
}
test_function2 <- function() {
#access variable test here
}
第 2 部分
test_function <- function() {
print(sys.nframe())
x <- 5
list2env(list(test = x), envir = sys.frame(sys.nframe()))
test_function2()
}
test_function2 <- function(envir = parent.frame()) {
print(sys.nframe())
x <- envir$test
if (x<5) {
test_function3()
} else{
test_function2()
}
}
test_function3 <- function(envir = parent.frame()) {
}
在 test_function2() 的第二次调用中,我无法访问测试。
谁能告诉我如何访问 test_function2 中的变量 test? 提前致谢!
【问题讨论】: