I'm having trouble understanding why this piece of R code works the way it does. Here is a simple function:
weird_ls <- function() {
some_env <- new.env()
assign("a", value = 1, envir = some_env)
assign("b", value = 2, envir = some_env)
out <- function() {
ls(envir = some_env)
}
return(out)
}
And here is what happens when I call it:
> f <- weird_ls()
> f()
[1] "a" "b"
But why is it the case? In my understanding, the object f is a function defined in the global environment. When I call it, it runs in its own (new) runtime environment where it executes ls(envir = some_env). But there is no some_env object in the runtime environment or in its parent, the global environment. In fact, some_env was only defined during the assignment f <- weird_ls() but was never returned by the function.
Can anyone help shed some light? I suspect I might be missing something on the rules of scoping and environments.
The environment, including all its contents, in which a function was defined is part of the function by so passing out the function we are also passing out that environment. The objects in the frame that was created while the function is run are only garbage collected if nothing points to them. Simplifying the example