Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

"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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

a = 1
b = 'bar'
c = 3

>Solution :

Create a list from env and then use list2env

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

or equivalently as a pipeline

env |> as.list() |> list2env(.GlobalEnv)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading