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
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.
The object .BaseNamespaceEnv
is the namespace environment for
the base package. The environment of the base package itself is
available as baseenv()
.
If one follows the chain of enclosures found by repeatedly calling
parent.env
from any environment, eventually one reaches the
empty environment emptyenv()
, into which nothing may
be assigned.
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 environment
, is.environment
,
baseenv
, emptyenv
and globalenv
are
primitive functions.
System environments, such as the base, global and empty environments,
have names as do the package and namespace environments and those
generated by attach()
. Other environments can be named by
giving a "name"
attribute, but this needs to be done with care
as environments have unusual copying semantics.