goweon
goweon

Reputation: 1344

"unpack" contents of an R environment object to current working environment

I want to achieve an effect similar to saving and loading .RData files like the following code, but without writing anything out to a file, and using environments instead.

a <- 1
c <- 3
save('a', 'c', file="file.RData")
a <- 'foo'
b <- 'bar'
load("file.RData")

So lets say I have some variables stored within an environment, some which share names of variables in the working environment

a <- 'foo'
b <- 'bar'
env <- new.env()
env$a <- 1
env$c <- 3

I want to unpack all the contents of env into the current environment, possibly overwriting some variables, such that the final values of each variable are

a = 1
b = 'bar'
c = 3

Upvotes: 1

Views: 89

Answers (1)

G. Grothendieck
G. Grothendieck

Reputation: 269526

Create a list from env and then use list2env

list2env(as.list(env), .GlobalEnv)

or equivalently as a pipeline

env |> as.list() |> list2env(.GlobalEnv)

Upvotes: 4

Related Questions