as_env() coerces named vectors (including lists) to an environment. It first checks that x is a dictionary (see is_dictionaryish()). If supplied an unnamed string, it returns the corresponding package environment (see pkg_env()).

as_env(x, parent = NULL)



An object to coerce.


A parent environment, empty_env() by default. This argument is only used when x is data actually coerced to an environment (as opposed to data representing an environment, like NULL representing the empty environment).


If x is an environment and parent is not NULL, the environment is duplicated before being set a new parent. The return value is therefore a different environment than x.


# Coerce a named vector to an environment: env <- as_env(mtcars) # By default it gets the empty environment as parent: identical(env_parent(env), empty_env())
#> [1] TRUE
# With strings it is a handy shortcut for pkg_env(): as_env("base")
#> <environment: base>
#> <environment: package:rlang> #> attr(,"name") #> [1] "package:rlang" #> attr(,"path") #> [1] "/Users/lionel/Dropbox/R/hadley/rlang"
# With NULL it returns the empty environment: as_env(NULL)
#> <environment: R_EmptyEnv>