How can I load an object into the variable name that I specify from the R data file?

When you save a variable in the R data file using save , it is saved under any name that it had in the session that saved it. When I switch later to download it from another session, it loads with the same name that cannot be recognized by the loading script. This name can overwrite an existing variable with the same name in the boot session. Is there a way to safely load an object from a data file into the specified variable name without the risk of knocking down existing variables?

Example:

Saving Session:

 x = 5 save(x, file="x.Rda") 

Session Download:

 x = 7 load("x.Rda") print(x) # This will print 5. Oops. 

How I want it to work:

 x = 7 y = load_object_from_file("x.Rda") print(x) # should print 7 print(y) # should print 5 
+84
r rdata
Apr 7 2018-11-11T00:
source share
7 answers

If you just save one object, do not use the .Rdata file, use the .RDS file:

 x <- 5 saveRDS(x, "x.rds") y <- readRDS("x.rds") all.equal(x, y) 
+81
Apr 7 2018-11-11T00:
source share

You can create a new environment, upload the .rda file to this environment and get an object from there. However, this imposes some restrictions: either you know what the original name is for your object, or there is only one object stored in the file.

This function returns an object loaded from the supplied .rda file. If there is more than one object in the file, an arbitrary is returned.

 load_obj <- function(f) { env <- new.env() nm <- load(f, env)[1] env[[nm]] } 
+33
Apr 7 2018-11-11T00:
source share

I am using the following:

 loadRData <- function(fileName){ #loads an RData file, and returns it load(fileName) get(ls()[ls() != "fileName"]) } d <- loadRData("~/blah/ricardo.RData") 
+31
Aug 22 '14 at 21:44
source share

You can also try something like:

 # Load the data, and store the name of the loaded object in x x = load('data.Rsave') # Get the object by its name y = get(x) # Remove the old object since you've stored it in y rm(x) 
+22
Sep 17 '13 at 16:13
source share

In case someone is looking for this with a simple source file and not with a saved Rdata / RDS / Rda file, the solution is very similar to the solution provided by @Hong Ooi

 load_obj <- function(fileName) { local_env = new.env() source(file = fileName, local = local_env) return(local_env[[names(local_env)[1]]]) } my_loaded_obj = load_obj(fileName = "TestSourceFile.R") my_loaded_obj(7) 

Print

[1] "The arg value is 7"

And in a separate source file TestSourceFile.R

 myTestFunction = function(arg) { print(paste0("Value of arg is ", arg)) } 

Again, this solution only works if there is exactly one file, if there are more, then it will simply return one of them (perhaps the first, but this is not guaranteed).

+2
Sep 28 '16 at 16:33
source share

I am extending the answer from @ricardo to allow the selection of a specific variable if the .Rdata file contains several variables (since my credits are small for editing the answer). It adds a few lines to read user input after listing the variables contained in the .Rdata file.

 loadRData <- function(fileName) { #loads an RData file, and returns it load(fileName) print(ls()) n <- readline(prompt="Which variable to load? \n") get(ls()[as.integer(n)]) } select_var <- loadRData('Multiple_variables.Rdata') 

0
Feb 15 '19 at 14:23
source share

Single Object Rdata File

 assign('newname', get(load('~/oldname.Rdata'))) 
0
Aug 16 '19 at 18:01
source share



All Articles