Not sure of whether this fully answers your question, anyway:
.GlobalEnv
is the user's workspace, and is not the same as the working directory. It is where user-defined objects accumulate. From the help page of the function environment()
:
The global environment .GlobalEnv
, more often known as the user's
workspace, is the first item on the search path. It can also be
accessed by globalenv()
.
From the R language definition, the definition of environment:
Environments can be thought of as consisting of two things: a frame,
which is a set of symbol-value pairs, and an enclosure, a pointer to
an enclosing environment. When R looks up the value for a symbol the
frame is examined and if a matching symbol is found its value will be
returned. If not, the enclosing environment is then accessed and the
process repeated. Environments form a tree structure in which the
enclosures play the role of parents. The tree of environments is
rooted in an empty environment, available through emptyenv()
, which
has no parent.
The base package is not contained in .GlobalEnv
, but in a different environment called .BaseNamespaceEnv
, and can be accessed also by baseenv()
. You verify that the two environments contain different objects by comparing the output of ls(globalenv())
and s(baseenv())
.
When R looks up for something, it starts in the global enviroment, then if it doesn't find it there, it moves up to the enclosing (parent) environment, and so on. If you use the parent.env()
function to follow the whole chain of parent environments, you will see that the base package is searched last, after any other loaded packages and just before the empty environment (into which nothing can be assigned).
e_ <- .GlobalEnv
while( !identical(e_, emptyenv()) )
{
e_ <- parent.env(e_)
print(e_)
}