Coerce to an environmentSource:
as_environment() coerces named vectors (including lists) to an
environment. The names must be unique. If supplied an unnamed
string, it returns the corresponding package environment (see
An object to coerce.
A parent environment,
empty_env()by default. This argument is only used when
xis data actually coerced to an environment (as opposed to data representing an environment, like
NULLrepresenting the empty environment).
x is an environment and
parent is not
environment is duplicated before being set a new parent. The return
value is therefore a different environment than
# Coerce a named vector to an environment: env <- as_environment(mtcars) # By default it gets the empty environment as parent: identical(env_parent(env), empty_env()) #>  TRUE # With strings it is a handy shortcut for pkg_env(): as_environment("base") #> <environment: base> as_environment("rlang") #> <environment: package:rlang> #> attr(,"name") #>  "package:rlang" #> attr(,"path") #>  "/home/runner/work/_temp/Library/rlang" # With NULL it returns the empty environment: as_environment(NULL) #> <environment: R_EmptyEnv>