environment
Environment Access
Description
These functions are related to R environments.
Usage
environment(fun = NULL)
environment<-(fun, value)
globalenv()
emptyenv()
baseenv()
is.environment(x)
new.env(hash = TRUE, parent = parent.frame(), size = 29L)
parent.env(env)
parent.env<-(env, value)
environmentName(env)
Arguments
fun |
the function. The default is NULL.
|
value |
the environment to associate with the function specified by the fun.
|
x |
an object.
|
env |
an environment.
|
hash |
this argument is ignored.
|
parent |
the enclosing environment.
|
size |
this argument is ignored.
|
Details
environment() works as follows:
- If fun is a function closure, environment returns the enclosing environment of the function closure.
- If fun is NULL, it returns the current environment.
- If fun is not NULL and is not a function closure, it returns the value of the
.Environment attribute of fun.
environment<-(fun, value) sets the environment specified by
value and returns the modified function closure, as follows:
- If fun is a function closure and value is NULL, it returns an error.
- If fun is a function closure and value is an environment,
it sets the enclosing environment of the function to value.
- If fun is not a function, and value is a valid environment or NULL,
it sets the .Environment attribute of fun to value.
- If fun is not a function and value is not an environment, it returns an error.
parent.env() checks whether
env is an environment. It works as follows:
- If env is not an environment, it returns an error.
- If env is equal to emptyenv(), it returns an error indicating that
the empty environment has no parent.
- If env is an environment, it returns the parent environment of env.
new.env() creates the new environment with the specified parent environment.
It returns an error if
parent is not an environment.
parent.env <- (env, value) sets the parent environment of the environment
env
to
value, and returns
env.
It returns errors in the following cases:
- If env is not an environment.
- If env is equal to emptyenv().
It is not possible to set the parent of the empty environment.
- If value is NULL.
- If value is not an environment.
environmentName() returns the character string representing the name of the
environment, as follows:
- If env is not an environment, it returns an empty character string.
- If env is the global environment, it returns the string "R_GlobalEnv".
- If env is the base environment, it returns the string "base".
- If env is emptyenv(), it returns the character string "R_EmptyEnv".
- If env is the package environment, it returns the package name by accessing the name attribute of the specified package prefixed with package:.
- If env is the namespace environment, it returns the name attribute of the specified namespace
environment (if it is not NULL).
Value
environment | returns the environment name associated with the function fun.
If fun is NULL, it returns the current environment where it is executed. |
baseenv | returns the environment of the base package. |
globalenv | returns the global environment. |
emptyenv | returns the empty environment. |
is.environment | returns TRUE if x is an environment; otherwise it returns FALSE. |
new.env | returns a new environment. |
parent.env | returns the parent environment of env. |
parent.env <- | returns env. |
environmentName | returns the name of environment env. |
Examples
environment(lm)
environment()
fn <- function(x) x+1
environment(fn) <- new.env()
environment(fn)
x <- 1
attr(x, ".Environment") <- "myenv"
environment(x)
# [1] "myenv"
baseenv()
globalenv()
emptyenv()
new.env(parent = emptyenv())
is.environment(new.env(parent = emptyenv()))
parent.env(globalenv())
parent.env(new.env(parent = emptyenv()))
environmentName(globalenv())