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

49,924

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')))
Share:
49,924

Related videos on Youtube

Ryan C. Thompson
Author by

Ryan C. Thompson

Updated on July 08, 2022

Comments

  • Ryan C. Thompson
    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
    Ryan C. Thompson about 13 years
    Is the new.env really necessary? Doesn't the function call itself provide a temporary environment?
  • Wojciech Sobala
    Wojciech Sobala about 13 years
    You can return whole environment (return(env) or return(environment()) when you modify function according to @Ryan suggestion).
  • hadley
    hadley about 13 years
    Updated to reflect that in 2.13 these are no longer experimental.
  • Ryan C. Thompson
    Ryan C. Thompson about 13 years
    Does that mean that they're fully supported, just like .Rdata files?
  • hadley
    hadley about 13 years
    If you use the function environment it will contain f and have a parent. Probably not what you want
  • Ryan C. Thompson
    Ryan C. Thompson almost 12 years
    Since these are no longer experimental, I'm marking this as the accepted answer. This is what I've been using.
  • Ryan C. Thompson
    Ryan C. Thompson over 10 years
    The point is to avoid clobbering the value of x when loading.
  • Aleksandr Blekh
    Aleksandr Blekh almost 10 years
    Do saveRDS and readRDS, correspondingly, save and restore all object's attributes, including ones created by an application (via attr)? I tried to use this approach instead of save and load, 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
    Trevor Nederlof almost 9 years
    This 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
    Repmat about 8 years
    Something like this belongs in base R
  • Sander W. van der Laan
    Sander W. van der Laan about 7 years
    OMG. This is awesome. Just what I need! #loveit
  • nnachefski
    nnachefski over 6 years
    Can you please expand on the logic of the GET statement? Why not match to FILENAME instead of excluding?
  • Ryan C. Thompson
    Ryan C. Thompson over 4 years
    Won'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
    Gerhard Burger over 2 years
    For an RData with multiple objects, use mget instead of get

Related