Table of Contents
- Advanced R: Environments
new.env(parent = parent.frame())
ls(name, all.names = FALSE)
ls.str(name, all.names = FALSE)
with(data, expr, ...)
within(data, expr, ...)
Advanced R: Environments tutorial
- Every environment except the empty environment, the ultimate ancestor of all environments, has a parent.
- If a name is not found in an environment, then R will look in its parent (and so on).
- the environment in which you normally work.
- the environment of the
basepackage. Its parent is the
- is the ultimate ancestor of all environments
<environment: R_GlobalEnv>  "a" "b" "c" <environment: 0x7fd053291808> a : num 1 b : num 2 c : num 3  3  "b" "c"  FALSE  TRUE  TRUE
There are different kinds of environments related to functions
- binding environment
- binding a function to a name with
<-defines a binding environment
- execution environment
- calling a function creates an ephemeral execution environment that stores variables created during execution
- calling environment
- Every execution environment is associated with a calling environment, which tells you where the function was called
The enclosing environment
- The environment where the function was created.
This works because every package has two environments associated with it: the package environment and the namespace environment.
- package environment
- every publicly accessible function, and is placed on the search path.
- namespace environment
- contains all functions (including internal functions), and its parent environment is a special imports environment that contains bindings to all the functions that the package needs.
Every exported function in a package is bound into the package environment, but enclosed by the namespace environment. This complicated relationship is illustrated by the following diagram:
- Every time a function is called, a new environment is created to host execution.
- The parent of the execution environment is the enclosing environment of the function.
- Once the function has completed, this environment is thrown away.
When you create a function inside another function,
- The enclosing environment of the child function is the execution environment of the parent
- which means the execution environment is no longer ephemeral.
- R’s regular scoping rules only use the enclosing parent
parent.frame()allows you to access the calling parent.
 10  20
Biding names to values
- Names usually consist of letters, digits,
_, and can’t begin with
- There are two other special types of binding, delayed and active:
<-creates a variable in the current environment.
<<-modifies an existing variable found by walking up the parent environments. if not found any, creates a variable in
- A delayed binding creates and stores a promise to evaluate the expression when needed.
- We can create delayed bindings with the special assignment operator
%<d-%, provided by the
- Active are not bound to a constant object. Instead, they’re re-computed every time they’re accessed
- To create active binding, use
user system elapsed 0 0 0 user system elapsed 0 0 1 user system elapsed 0 0 0
 0.1530638  0.4654055
- Unlike most objects in R, when you modify an environment, it does not make a copy.
- Since environments have reference semantics, you’ll never accidentally create a copy. This makes it a useful vessel for large objects.
search() lists all parents of the global environment.
 ".GlobalEnv" "package:stats" "package:graphics"  "package:grDevices" "package:utils" "package:datasets"  "package:methods" "Autoloads" "package:base"
new.env(parent = parent.frame()) reference
ls(name, all.names = FALSE) reference
- this accepts an
FALSE, names which begin with a
 "x" "y"  ".hidden" "x" "y"
ls.str(name, all.names = FALSE) reference
str() of all names from
x : num 10 y : num 20
parent.env()returns the enclosing environment
parent.frame()returns the calling environment
$parent.env  "f" $parent.frame  "global"
with(data, expr, ...) reference
within(data, expr, ...) reference
- data to use for constructing an environment, may be an environment, a list, a data frame, etc.
- expression to evaluate
within() makes a modified copy of
with() returns the evaluated
within() returns the modified copy.
x : num 10
$x  20