The Global Environment

When a user starts a new session in R, the R system creates a new environment for objects created during that session. This environment is called the global environment. The global environment is not actually the root of the tree of environments. It’s actually the last environment in the chain of environments in the search path. Here’s the list of parent environments for the global environment in my R installation:

> x <- .GlobalEnv
> while (environmentName(x) != environmentName(emptyenv())) {
+      print(environmentName(parent.env(x))); x <- parent.env(x)}
[1] "tools:RGUI"
[1] "package:stats"
[1] "package:graphics"
[1] "package:grDevices"
[1] "package:utils"
[1] "package:datasets"
[1] "package:methods"
[1] "Autoloads"
[1] "base"
[1] "R_EmptyEnv"

Every environment has a parent environment except for one: the empty environment. All environments chain back to the empty environment.

Get R in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.