How can I load an object into a variable name that I specify from an R data file?
Solution 1
If you're just saving a single object, don't use an .Rdata
file, use an .RDS
file:
x <- 5
saveRDS(x, "x.rds")
y <- readRDS("x.rds")
all.equal(x, y)
Solution 2
I use the following:
loadRData <- function(fileName){
#loads an RData file, and returns it
load(fileName)
get(ls()[ls() != "fileName"])
}
d <- loadRData("~/blah/ricardo.RData")
Solution 3
You can create a new environment, load the .rda file into that environment, and retrieve the object from there. However, this does impose some restrictions: either you know what the original name for your object is, or there is only one object saved in the file.
This function returns an object loaded from a supplied .rda file. If there is more than one object in the file, an arbitrary one is returned.
load_obj <- function(f)
{
env <- new.env()
nm <- load(f, env)[1]
env[[nm]]
}
Solution 4
You could 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)
Solution 5
Rdata file with one object
assign('newname', get(load('~/oldname.Rdata')))
Related videos on Youtube
Ryan C. Thompson
Updated on July 08, 2022Comments
-
Ryan C. Thompson almost 2 years
When you save a variable in an R data file using
save
, it is saved under whatever name it had in the session that saved it. When I later go to load it from another session, it is loaded with the same name, which the loading script cannot possibly know. This name could overwrite an existing variable of the same name in the loading session. Is there a way to safely load an object from a data file into a specified variable name without risk of clobbering existing variables?Example:
Saving session:
x = 5 save(x, file="x.Rda")
Loading session:
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
-
Ryan C. Thompson about 13 yearsIs the
new.env
really necessary? Doesn't the function call itself provide a temporary environment? -
Wojciech Sobala about 13 yearsYou can return whole environment (return(env) or return(environment()) when you modify function according to @Ryan suggestion).
-
hadley about 13 yearsUpdated to reflect that in 2.13 these are no longer experimental.
-
Ryan C. Thompson about 13 yearsDoes that mean that they're fully supported, just like .Rdata files?
-
hadley about 13 yearsIf you use the function environment it will contain f and have a parent. Probably not what you want
-
Ryan C. Thompson almost 12 yearsSince these are no longer experimental, I'm marking this as the accepted answer. This is what I've been using.
-
Ryan C. Thompson over 10 yearsThe point is to avoid clobbering the value of x when loading.
-
Aleksandr Blekh almost 10 yearsDo
saveRDS
andreadRDS
, correspondingly, save and restore all object's attributes, including ones created by an application (viaattr
)? I tried to use this approach instead ofsave
andload
, trying to find a workaround for my problem. Howver, it doesn't seem to be the case, unless I'm doing something wrong: stackoverflow.com/questions/23701195/…. -
Trevor Nederlof almost 9 yearsThis is a great little function. Was having issues with trying to load things in (didnt want to just put them in the global environment as I know that was going to problems.
-
Repmat about 8 yearsSomething like this belongs in base R
-
Sander W. van der Laan about 7 yearsOMG. This is awesome. Just what I need! #loveit
-
nnachefski over 6 yearsCan you please expand on the logic of the GET statement? Why not match to FILENAME instead of excluding?
-
Ryan C. Thompson over 4 yearsWon't this load the object into the old name, and then also assign it to the new name as well? That won't help if I'm worried about the possibility of overwriting an existing variable.
-
Gerhard Burger over 2 yearsFor an RData with multiple objects, use
mget
instead ofget