Get, set, test for and create environments.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
an environment to associate with the function
an arbitrary R object.
a logical, if
an environment to be used as the enclosure of the environment created.
an integer specifying the initial size for a hashed
environment. An internal default value will be used if
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
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
user's workspace, is the first item on the search path. It can also
be accessed by
globalenv(). On the search path, each item's
enclosure is the next item.
.BaseNamespaceEnv is the namespace environment for
the base package. The environment of the base package itself is
If one follows the chain of enclosures found by repeatedly calling
parent.env from any environment, eventually one reaches the
emptyenv(), into which nothing may
The replacement function
parent.env<- is extremely dangerous as
it can be used to destructively change environments in ways that
violate assumptions made by the internal C code. It may be removed
in the near future.
The replacement form of
System environments, such as the base, global and empty environments,
have names as do the package and namespace environments and those
attach(). Other environments can be named by
"name" attribute, but this needs to be done with care
as environments have unusual copying semantics.
fun is a function or a formula then
returns the environment associated with that function or formula.
NULL then the current evaluation environment is
The replacement form sets the environment of the function or formula
fun to the
TRUE if and only if
obj is an
new.env returns a new (empty) environment with (by default)
enclosure the parent frame.
parent.env returns the enclosing environment of its argument.
parent.env<- sets the enclosing environment of its first
environmentName returns a character string, that given when
the environment is printed or
"" if it is not a named environment.
env.profile returns a list with the following components:
size the number of chains that can be stored in the hash table,
nchains the number of non-empty chains in the table (as
counts an integer vector
giving the length of each chain (zero for empty chains). This
function is intended to assess the performance of hashed environments.
env is a non-hashed environment,
NULL is returned.
For the performance implications of hashing or not, see https://en.wikipedia.org/wiki/Hash_table.
envir argument of
ls may be used to view the objects in an environment,
ls.str may be useful for an overview.
sys.source can be used to populate an environment.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
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)))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.