0

From the load help page:

## save all data
xx <- pi # to ensure there is some data

save(list = ls(all = TRUE), file= "all.RData")
rm(xx)

## restore the saved values to the current environment
local({
   load("all.RData")
   ls()
})

xx <- exp(1:3)
## restore the saved values to the user's workspace
load("all.RData") ## which is here *equivalent* to
## load("all.RData", .GlobalEnv)
## This however annihilates all objects in .GlobalEnv with the same names !
xx # no longer exp(1:3)
rm(xx)
attach("all.RData") # safer and will warn about masked objects w/ same name in .GlobalEnv

I was wondering: is there any way other than attaching data to prevent load() from "annihilating all objects in .GlobalEnv with the same names"? Thank you in advance

simone
  • 577
  • 1
  • 7
  • 15
  • It's better to use RDS for storing and loading data (`readRDS`, `saveRDS`, etc). `attach` is really bad. – Hack-R Sep 29 '16 at 17:24
  • I want to avoid using attach, that's why the question – simone Sep 29 '16 at 17:28
  • Right, so that's why I answered recommending RDS – Hack-R Sep 29 '16 at 17:28
  • right, makes sense :) – simone Sep 29 '16 at 17:30
  • 1
    [this](http://stackoverflow.com/questions/5577221/how-can-i-load-an-object-into-a-variable-name-that-i-specify-from-an-r-data-file) and [this](http://stackoverflow.com/questions/2520780/determining-name-of-object-loaded-in-r) somewhat related – David Arenburg Sep 29 '16 at 17:31
  • 2
    As well you could create a new environment with newEnvironment<- new.env() and specify the environment in the load command, like load("all.rdata", envir=newEnvironment) – Benjamin Mohn Sep 29 '16 at 17:33

0 Answers0