z <- 1
a <- function() {
print(parent.frame())
eval(quote({print(z)}))
}
b <- function() {
z <- 2
print(environment())
a()
}
b()
# output:
<environment: 0x55f0020b1af0>
<environment: 0x55f0020b1af0>
[1] 1
According to documentation:
Usage
eval(expr, envir = parent.frame(),
enclos = if(is.list(envir) || is.pairlist(envir))
parent.frame() else baseenv())
Since the envir
argument defaults to parent.frame()
, which is the environment of function b
, it is the z
inside of b
that is supposed to be printed. But rather it is the z
in the global environment that is printed.