I'm new to R and trying to clear up some confusion about environments. My questions are closely related so I'll just ask them all here.
Question 1: what is the difference between a 'parent' environment (also called 'enclosure', and accessed via parent.env), and the 'parent frame', accessed via parent.frame(n=1)? For example:
f = function() { print(environment());
g = function() {env = environment(); print(env); print(parent.env(env)); print(parent.frame(n=1));}
g();
}
f();
Calling f will give the same 'enclosure' and 'parent frame' for the local environment of g, namely the local environment of f. So what's the difference?
Question 2: the enclosure of an environment, as I understand it, is the next place that R looks to find the definition of something when that environment can't answer it. But it doesn't seem to make any difference once you attach an environment and place it in a certain spot in the search path. Consider
Env1 = new.env(); Env2 = new.env();
parent.env(.GlobalEnv); parent.env(Env2); parent.env(Env1);
x = "Global_value_x";
Env1$x = "Env1_value_x"; Env1$y = "Env1_value_y";
Env2$x = "Env2_value_x"; Env2$y = "Env2_value_y"; Env2$z = "Env2_value_z";
attach(Env2); attach(Env1);
parent.env(.GlobalEnv); parent.env(Env2); parent.env(Env1);
x; y; z;
Compare the output of lines 2 and 7. The enclosure of .GlobalEnv has been changed appropriately to Env1, since Env1 is now in position 2 in the search path, but the enclosure of Env1 is still .GlobalEnv. And when you call x, y and z, the z-value of Env2 is what is returned, indicating that Env1 is passing the question on to Env2. But, shouldn't it be passing the question back to its enclosure, namely .GlobalEnv? Which would be a bad thing of course, but still, what is the point of saying that .GlobalEnv is the enclosure of Env1, when Env1 is actually passing questions to Env2?
Question 3: Consider
x = "Global_value_x";
Env1 = new.env(); Env2 = new.env();
Env2$x = "Env2_value_x";
attach(Env2); attach(Env1);
x;
Env1$x;
After executing line 4, R reminds me that x is already defined in the global environment, and so it's value within Env2 has been masked. Line 5 of course returns the global value of x. Is there any way to supersede this? In other words, place Env1 at position 1 in the search path? It seems like the entire point of having an environment would be to "live" in it temporarily, to make it your new default operating space, and its values should take precedent over other stuff you've done. I realize that this is essentially what happens when a function is called, creating a temporary local environment for its variables and whatnot. Is there any way to make this happen outside of any function, in the general workspace?
Also, notice that executing line 6 returns NULL, since x is undefined within Env1. But shouldn't then it return "Env2_value_x", since Env2 is next in line in the search path, and x is actually defined there?
and lastly
Question 4: is there anyway to COMPLETELY reset R within Rstudio, search path and all? rm(list=ls()) of course clears the variables, and you can type detach() a bunch of times to clear the search path, but it's annoying. Is there a general restart command within R?
Thanks a bunch for any help.