I would like to ask something about R’s environments:
In the next simple code I create the local variable “v1”.
“f1” lays in the global environment, as we can see when we type “environment(f1)”.
My question is how can we access “v1” from within the R console. “v1$f1” is not working. Is there an explanation for this?
rm(list = ls())
f1 <- function() {
v1 <- 1
}
environment(f1)
Next, if I create the environment “e1”
e1 <- new.env()
and I put “f1” inside “e1”
environment(f1) <- e1
When I use “ls(e1)” I do not receive “f1”. Does anybody know why?
ls(e1)
Thank you in advance
The local variable
v1does not exists until you call the functionf1, and then the environment where it lives is typically destroyed whenf1exits. But you can get hold of it if you modifyf1:For your second question, you assigned
e1tof1, not the other way around. Sof1has the environmente1where it looks for things. If you specify a parent environment tonew.env, that’s where it will continue looking for stuff: