list2env {base} | R Documentation |
From a named list x
, create an
environment
containing all list components as objects, or
“multi-assign” from x
into a pre-existing environment.
list2env(x, envir = NULL, parent = parent.frame(),
hash = (length(x) > 100), size = max(29L, length(x)))
x |
a |
envir |
an |
parent |
(for the case |
hash |
(for the case |
size |
(in the case |
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.
An environment
, either newly created (as by
new.env
) if the envir
argument was NULL
,
otherwise the updated environment envir
. Since environments
are never duplicated, the argument envir
is also changed.
Martin Maechler
environment
, new.env
,
as.environment
; further, assign
.
The (semantical) “inverse”: as.list.environment
.
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, prob = 0.2))),
envir = e)
utils::ls.str(e)