I was playing around with R and noticed some inconsistencies with the Global Environment surrounding function calls differing from the actual global environment.
Consider the following:
> test = function ()
+ {
+ print(environmentName(as.environment(-1)))
+ print(ls(as.environment(-1)))
+ print(environmentName(.GlobalEnv))
+ print(ls(.GlobalEnv))
+ as.environment(-1)
+ }
> foo = 1
> ls()
[1] "foo" "test"
> test()
[1] ""
[1] "doTryCatch" "expr" "handler" "name" "parentenv"
[1] "R_GlobalEnv"
[1] "foo" "test"
<environment: R_GlobalEnv>
Within the function call, as.environment(-1)
returns an environment that claims it is <environment: R_GlobalEnv>
but when calling environmentName
on said environment, its name is an empty character. Furthermore, the contents of it differ from what is in the true global environment. What exactly is going on here?
I first noticed the error using mget
within a call, as a variable defined globally could not be found. This seems counter-intuitive because normally when referencing a variable within a function, R will search upwards in the enclosing environments until it finds a definition for a variable, including the global environment.
environmentName
is not needed to show this difference. The two print statements here give different results:test <- function() { e <- as.environment(-1); print(e); print(as.environment(-1)) }; test()
print(as.environment(-1)); I(as.environment(-1)); c(as.environment(-1))
. They all give different results, presumably related to how and where they evaluate their supplied arguments, though it's not really clear to me what's going on. Relevant C-level source code is here and here. (The latter bit clued me in to the equivalence ofas.environment(-1)
andpos.to.env(-1)
.)