ID EN
Environment & Scope

environment

R Base 3.6.2

Get, set, test for and create environments.

Syntax

R
environment(fun = NULL)
environment(fun) &lt;- value<p></p><p>is.environment(x)</p><p>.GlobalEnv
globalenv()
.BaseNamespaceEnv</p><p>emptyenv()
baseenv()</p><p>new.env(hash = TRUE, parent = parent.frame(), size = 29L)</p><p>parent.env(env)
parent.env(env) &lt;- value</p><p>environmentName(env)</p><p>env.profile(env)</p>

Arguments

Parameter Description
fun a function, a formula, or NULL, which is the default.
value an environment to associate with the function
x an arbitrary R object.
hash a logical, if TRUE the environment will use a hash table.
parent an environment to be used as the enclosure of the environment created.
env an environment
size an integer specifying the initial size for a hashed environment. An internal default value will be used if size is NA or zero. This argument is ignored if hash is FALSE.

Return Value

If fun is a function or a formula then environment(fun) returns the environment associated with that function or formula. If fun is NULL then the current evaluation environment is returned. The replacement form sets the environment of the function or formula fun to the value given. is.environment(obj) returns TRUE if and only if obj is an environment. new.env returns a new (empty) environment with (by default) enclosure the parent frame. parent.env returns the enclosing environment of its argume

Details

Environments consist of a frame, or collection of named objects, and a pointer to an enclosing environment. The most common example is the frame of variables local to a function call; its enclosure is the environment where the function was defined (unless changed subsequently). The enclosing environment is distinguished from the parent frame: the latter (returned by parent.frame) refers to the environment of the caller of a function. Since confusion is so easy, it is best never to use ‘parent’ in connection with an environment (despite the presence of the function parent.env). When get or exists search an environment with the default inherits = TRUE, they look for the variable in the frame, then in the enclosing frame, and so on. The global environment .GlobalEnv, more often known as the u

Examples

Example
R
# NOT RUN {
f <- function() "top level function"

##-- all three give the same:
environment()
environment(f)
.GlobalEnv

ls(envir = environment(stats::approxfun(1:2, 1:2, method = "const")))

is.environment(.GlobalEnv) # TRUE

e1 <- new.env(parent = baseenv())  # this one has enclosure package:base.
e2 <- new.env(parent = e1)
assign("a", 3, envir = e1)
ls(e1)
ls(e2)
exists("a", envir = e2)   # this succeeds by inheritance
exists("a", envir = e2, inherits = FALSE)
exists("+", envir = e2)   # this succeeds by inheritance

eh <- new.env(hash = TRUE, size = NA)
with(env.profile(eh), stopifnot(size == length(counts)))
# }

See Also

For the performance implications of hashing or not see https://en.wikipedia.org/wiki/Hash_table. The envir argument of eval get and exists. ls may be used to view the objects in an environment and hence ls.str may be useful for an overview. sys.source can be used to populate an environment.