[ RDocumentation ](/)

Moon  [ ](https://github.com/datacamp/rdocumentation-2.0) [ Learn R
](https://www.datacamp.com/learn/r)

Search all packages and functions

[ base  (version  3.6.2  ) ](/packages/base/versions/3.6.2)

#  list2env: From A List, Build or Add To an Environment

##  Description

From a _named_ ` [ list ](/link/list?package=base&version=3.6.2) x ` , create
an ` [ environment ](/link/environment?package=base&version=3.6.2) `
containing all list components as objects, or “multi-assign” from ` x ` into a
pre-existing environment.

##  Usage

    
    
    list2env(x, envir = NULL, parent = parent.frame(),
             hash = (length(x) > 100), size = max(29L, length(x)))

##  Arguments

x

a ` [ list ](/link/list?package=base&version=3.6.2) ` , where ` [ names
](/link/names?package=base&version=3.6.2) (x) ` must not contain empty ( ` ""
` ) elements.

envir

an ` [ environment ](/link/environment?package=base&version=3.6.2) ` or ` NULL
` .

parent

(for the case ` envir = NULL ` ): a parent frame aka enclosing environment,
see ` [ new.env ](/link/new.env?package=base&version=3.6.2) ` .

hash

(for the case ` envir = NULL ` ): logical indicating if the created
environment should use hashing, see ` [ new.env
](/link/new.env?package=base&version=3.6.2) ` .

size

(in the case ` envir = NULL, hash = TRUE ` ): hash size, see ` [ new.env
](/link/new.env?package=base&version=3.6.2) ` .

##  Value

An ` [ environment ](/link/environment?package=base&version=3.6.2) ` , either
newly created (as by ` [ new.env ](/link/new.env?package=base&version=3.6.2) `
) if the ` envir ` argument was ` NULL ` , otherwise the updated environment `
envir ` . Since environments are never duplicated, the argument ` envir ` is
also changed.

##  Details

This will be very slow for large inputs unless hashing is used on the
environment.

Environments must have uniquely named entries, but named lists need not: where
the list has duplicate names it is the _last_ element with the name that is
used. Empty names throw an error.

##  See Also

` [ environment ](/link/environment?package=base&version=3.6.2) ` , ` [
new.env ](/link/new.env?package=base&version=3.6.2) ` , ` [ as.environment
](/link/as.environment?package=base&version=3.6.2) ` ; further, ` [ assign
](/link/assign?package=base&version=3.6.2) ` .

The (semantical) “inverse”: ` [ as.list.environment
](/link/as.list.environment?package=base&version=3.6.2) ` .

##  Examples

[ Run this code
](https://app.datacamp.com/workspace/preview?_tag=rdocs&rdocsPath=packages%2Fbase%2Fversions%2F3.6.2%2Ftopics%2Flist2env&utm_source=r-docs&utm_medium=docs&utm_term=list2env&utm_content=run_example_in_workspace)

    
    
    # NOT RUN {
    L <- list(a = 1, b = 2:4, p = pi, ff = gl(3, 4, labels = LETTERS[1:3]))
    e <- list2env(L)
    ls(e)
    stopifnot(ls(e) == sort(names(L)),
              identical(L$b, e$b)) # "$" working for environments as for lists
    
    ## consistency, when we do the inverse:
    ll <- as.list(e)  # -> dispatching to the as.list.environment() method
    rbind(names(L), names(ll)) # not in the same order, typically,
                               # but the same content:
    stopifnot(identical(L [sort.list(names(L ))],
                        ll[sort.list(names(ll))]))
    
    ## now add to e -- can be seen as a fast "multi-assign":
    list2env(list(abc = LETTERS, note = "just an example",
                  df = data.frame(x = rnorm(20), y = rbinom(20, 1, pr = 0.2))),
             envir = e)
    utils::ls.str(e)
    # }
    

Run the code above in your browser using  [ DataCamp Workspace
](https://app.datacamp.com/workspace/preview?_tag=rdocs&rdocsPath=packages%2Fbase%2Fversions%2F3.6.2%2Ftopics%2Flist2env&utm_source=r-docs&utm_medium=docs&utm_term=list2env&utm_content=run_example_in_workspace)

Powered by  [ DataCamp ](https://www.datacamp.com/)