Assuming that you really need to do this, the function parent.frame()
gives it.
foo <- function() {
parent.frame()$a
}
caller <- function() {
a <- 1
foo()
}
caller()
## [1] 1
however, normally one would write it like this (only foo
is changed) as it gives the desired functionality but also the flexibility to change the environment used.
foo <- function(envir = parent.frame()) {
envir$a
}
caller <- function() {
a <- 1
foo()
}
caller()
## [1] 1
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…