Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

as_environment

Coerce to an environment


Description

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 pkg_env()).

Usage

as_environment(x, parent = NULL)

Arguments

x

An object to coerce.

parent

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).

Details

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.

Life cycle

as_env() was soft-deprecated and renamed to as_environment() in rlang 0.2.0. This is for consistency as type predicates should not be abbreviated.

Examples

# 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())


# With strings it is a handy shortcut for pkg_env():
as_environment("base")
as_environment("rlang")

# With NULL it returns the empty environment:
as_environment(NULL)

rlang

Functions for Base Types and Core R and 'Tidyverse' Features

v0.4.11
MIT + file LICENSE
Authors
Lionel Henry [aut, cre], Hadley Wickham [aut], mikefc [cph] (Hash implementation based on Mike's xxhashlite), Yann Collet [cph] (Author of the embedded xxHash library), RStudio [cph]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.