\Header{environment}{Environment Access}
\alias{.BaseNamespaceEnv}{environment}
\alias{.GlobalEnv}{environment}
\alias{environment<\Rdash}{environment}
\alias{globalenv}{environment}
\alias{is.environment}{environment}
\alias{new.env}{environment}
\alias{parent.env}{environment}
\alias{parent.env<\Rdash}{environment}
\keyword{data}{environment}
\keyword{programming}{environment}
\begin{Description}\relax
Get, set, test for and create environments.
\end{Description}
\begin{Usage}
\begin{verbatim}
environment(fun = NULL)
environment(fun) <- value
is.environment(obj)
.GlobalEnv
globalenv()
.BaseNamespaceEnv
new.env(hash=FALSE, parent=parent.frame())
parent.env(env)
parent.env(env) <- value
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{fun}] a \code{\Link{function}}, a \code{\Link{formula}}, or
\code{NULL}, which is the default.
\item[\code{value}] an environment to associate with the function
\item[\code{obj}] an arbitrary \R{} object.
\item[\code{hash}] a logical, if \code{TRUE} the environment will be hashed
\item[\code{parent}] an environment to be used as the parent of the
environment created.
\item[\code{env}] an environment
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
The global environment \code{.GlobalEnv} is the first item on the
search path, more often known as the user's workspace.  It can also be
accessed by \code{globalenv()}.

The object \code{.BaseNamespaceEnv} is the namespace environment for
the base package.  The environment of the base package itself is
represented by \code{NULL}.

The replacement function \code{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.

\code{is.environment} is generic: you can write methods to handle
specific classes of objects, see \link{InternalMethods}.
\end{Details}
\begin{Value}
If \code{fun} is a function or a formula then \code{environment(fun)}
returns the environment associated with that function or formula.
If \code{fun} is \code{NULL} then the current evaluation environment is
returned.

The assignment form sets the environment of the function or formula
\code{fun} to the \code{value} given.

\code{is.environment(obj)} returns \code{TRUE} iff \code{obj} is an
\code{environment}.

\code{new.env} returns a new (empty) environment enclosed in the
parent's environment, by default.

\code{parent.env} returns the parent environment of its argument.

\code{parent.env<-} sets the parent environment of its first argument.
\end{Value}
\begin{SeeAlso}\relax
The \code{envir} argument of \code{\Link{eval}}.
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
##-- all three give the same:
environment()
environment(environment)
.GlobalEnv

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

is.environment(.GlobalEnv) # TRUE

e1 <- new.env(TRUE, NULL)  # these two have parent package:base.
e2 <- new.env(FALSE, NULL)
assign("a", 3, env=e2)
parent.env(e1) <- e2
get("a", env=e1)

\end{ExampleCode}
\end{Examples}

