2

I have some fundamental but, crucial questions after learning about search() function. I want answer for accurate meaning and relationships between packages, GlobalEnv, working directory and the workspace.

When I used the function, it says,

search() [1] ".GlobalEnv" "Autoloads" "package:base"

where ".GlobalEnv" is the workspace. Several sites say that workspace is the same as working directory. I wonder if ".GlobalEnv" is the same as the result of

getwd()

code. Also I wonder if all the packages like "Base" are included in the ".GlobalEnv" or stored in somewhere else.

Sunwoo Lim
  • 57
  • 1
  • 1
  • 4
  • The question might seem quite silly but I am asking cause the concept and relationship of workspace, globalenvironment, working directory is not stated thoroughly. – Sunwoo Lim Jan 13 '18 at 11:00
  • Here are two links which you may find helpful: [Environments](http://adv-r.had.co.nz/Environments.html) (from Hadley's Advanced R), [How R Searches and Finds Stuff](http://blog.obeautifulcode.com/R/How-R-Searches-And-Finds-Stuff/). – Henrik Jan 13 '18 at 11:40
  • Semi-related posts on SO: [Namespaces in R packages](https://stackoverflow.com/questions/4371181/namespaces-in-r-packages), [Writing robust R code: namespaces, masking and using the :: operator](https://stackoverflow.com/questions/10947159/writing-robust-r-code-namespaces-masking-and-using-the-operator). – Henrik Jan 13 '18 at 11:41

1 Answers1

1

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_)
}
matteo
  • 311
  • 1
  • 9