9

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.

2
  • 2
    It seems that 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() Mar 3, 2014 at 16:20
  • Also compare: 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 of as.environment(-1) and pos.to.env(-1).) Mar 3, 2014 at 22:54

1 Answer 1

4

This is a consequence of lazy evaluation:

test <- function () {
  e <- as.environment(-1)

  list(
    lazy = ls(as.environment(-1)), 
    eager = ls(envir = e)
  )
}

foo <- 1
test()
#> $lazy
#> [1] "doTryCatch" "expr"       "handler"    "name"       "parentenv" 
#> 
#> $eager
#> [1] "foo"  "test"
4
  • 3
    Could you elaborate a little bit more? Shouldn't as.environment(-1) get evaluated in the parent frame since it is an explicit argument to ls? How does lazyness come into this? I thought that was primarily an issue when defining closures without forcing arguments as the closure is defined. Sorry for being dense here.
    – BrodieG
    Mar 3, 2014 at 16:38
  • 3
    This still looks like a bug in R. as.environment(-1) should be evaluated relative to the promise's environment but it seems its not. Mar 3, 2014 at 16:38
  • 1
    It's definitely not a bug. I'll add some more explanation tomorrow when I'm at a computer
    – hadley
    Mar 4, 2014 at 5:49
  • 2
    @BrodieG it is evaluated in the parent environment, but I think environment(-1) uses not just the environment, but the context (which includes the calling stack) which is not captured in the promise.
    – hadley
    Mar 4, 2014 at 15:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.