env() and child_env() create new environments. env() always creates a child of the current environment while child_env() lets you specify a parent (see section on inheritance).

env(...)

child_env(parent, ...)

Arguments

...

Named values. These dots have explicit splicing semantics.

parent

A parent environment. Can be an object supported by as_env().

Environments as objects

Environments are containers of uniquely named objects. Their most common use is to provide a scope for the evaluation of R expressions. Not all languages have first class environments, i.e. can manipulate scope as regular objects. Reification of scope is one of the most powerful feature of R as it allows you to change what objects a function or expression sees when it is evaluated.

Environments also constitute a data structure in their own right. They are dictionaries of uniquely named objects, subsettable by name and modifiable by reference. This latter property (see section on reference semantics) is especially useful for creating mutable OO systems (cf the R6package and the ggprotosystem for extending ggplot2).

Inheritance

All R environments (except the empty environment) are defined with a parent environment. An environment and its grandparents thus form a linear hierarchy that is the basis for lexicalscoping in R. When R evaluates an expression, it looks up symbols in a given environment. If it cannot find these symbols there, it keeps looking them up in parent environments. This way, objects defined in child environments have precedence over objects defined in parent environments.

The ability of overriding specific definitions is used in the tidyeval framework to create powerful domain-specific grammars. A common use of overscoping is to put data frame columns in scope. See as_overscope() for technical details.

Reference semantics

Unlike regular objects such as vectors, environments are an uncopyable object type. This means that if you have multiple references to a given environment (by assigning the environment to another symbol with <- or passing the environment as argument to a function), modifying the bindings of one of those references changes all other references as well.

See also

scoped_env, env_has(), env_bind().

Examples

# env() creates a new environment which has the current environment # as parent env <- env(a = 1, b = "foo") env$b
#> [1] "foo"
identical(env_parent(env), get_env())
#> [1] TRUE
# child_env() lets you specify a parent: child <- child_env(env, c = "bar") identical(env_parent(child), env)
#> [1] TRUE
# This child environment owns `c` but inherits `a` and `b` from `env`: env_has(child, c("a", "b", "c", "d"))
#> [1] FALSE FALSE TRUE FALSE
env_has(child, c("a", "b", "c", "d"), inherit = TRUE)
#> [1] TRUE TRUE TRUE FALSE
# `parent` is passed to as_env() to provide handy shortcuts. Pass a # string to create a child of a package environment: child_env("rlang")
#> <environment: 0x7f979a3ce430>
env_parent(child_env("rlang"))
#> <environment: package:rlang> #> attr(,"name") #> [1] "package:rlang" #> attr(,"path") #> [1] "/Users/lionel/Dropbox/R/hadley/rlang"
# Or `NULL` to create a child of the empty environment: child_env(NULL)
#> <environment: 0x7f979b97e780>
env_parent(child_env(NULL))
#> <environment: R_EmptyEnv>
# The base package environment is often a good default choice for a # parent environment because it contains all standard base # functions. Also note that it will never inherit from other loaded # package environments since R keeps the base package at the tail # of the search path: base_child <- child_env("base") env_has(base_child, c("lapply", "("), inherit = TRUE)
#> [1] TRUE TRUE
# On the other hand, a child of the empty environment doesn't even # see a definition for `(` empty_child <- child_env(NULL) env_has(empty_child, c("lapply", "("), inherit = TRUE)
#> [1] FALSE FALSE
# Note that all other package environments inherit from base_env() # as well: rlang_child <- child_env("rlang") env_has(rlang_child, "env", inherit = TRUE) # rlang function
#> [1] TRUE
env_has(rlang_child, "lapply", inherit = TRUE) # base function
#> [1] TRUE
# Both env() and child_env() take dots with explicit splicing: objs <- list(b = "foo", c = "bar") env <- env(a = 1, !!! objs) env$c
#> [1] "bar"
# You can also unquote names with the definition operator `:=` var <- "a" env <- env(!!var := "A") env$a
#> [1] "A"