list2env {base}R Documentation

From A List, Build or Add To an Environment

Description

From a named list x, create an environment 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 = FALSE, size = 29L)

Arguments

x a list, where names(x) must contain valid names, i.e. at least of 1 character.
envir an environment or NULL.
parent (for the case envir = NULL): a parent frame aka enclosing environment, see new.env.
hash (for the case envir = NULL): logical indicating if the created environment should use hashing, see new.env.
size (in the case envir = NULL, hash = TRUE): hash size, see new.env.

Details

...

Value

an environment, either newly created (as with new.env) if the envir argument was NULL, otherwise the updated environment envir.

Author(s)

Martin Maechler

See Also

environment, new.env, as.environment; further, assign.

The (semantical) “inverse”: as.list.environment.

Examples

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":
e <- 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)

[Package base version 2.12.0 Index]