rglobal-variablesrdata

Preserve values given by identical object names from multiple RData images


I have multiple RData images saved. Each file contains the same number of objects with identical names across each file. How can I prepend the names of every object in every file so that I can load every file into my global environment without overwriting the objects from the previously loaded file?

For example, if I load "image1.RData", I get two objects in my global environment:

Object name Value
object1 a
object2 b

If I load "image2.RData", I get another two objects in my global environment:

Object name Value
object1 c
object2 d

The values for object1 and object2 given by "image1.RData" have been overwritten by the values from "image2.RData".

My goal is to be able to load each RData file and preserve the values for each object given by their respective file. Ideally, the object names from each file would be prepended with the name of their data file, such that my global environment would look something like this:

Object name Value
image1_object1 a
image1_object2 b
image2_object1 c
image2_object2 d

Is there a feasible way to make this happen? Prepending the object names isn't a necessary requirement as long as my goal is obtained, that's just what I thought made the most sense but I can't figure out how to do it.

Thanks in advance for the help.


Solution

  • Here is one solution. I'm sure there is a simpler approach out there, but this does the job nicely. It's based on the simple idea of:

    1. loading each data into a separate environment, so there is no overlap or overriding of variables.
    2. making changes to the names of variables to remove any duplicate names.
    3. then putting them all into the global environment (now that we know there are no duplicate name that will override each other)

    Here's my setup of your situation.

    object1 <- "a"
    object2 <- "b"
    save(object1, object2, file = "image1.rdata")
    object1 <- "c"
    object2 <- "d"
    save(object1, object2, file = "image2.rdata")
    rm(list = ls()) # remove all from global env
    

    Here is my solution:

    files <- fs::dir_ls(glob = "*.rdata") # get characther vector of paths to .rdata files
    
    for(i in 1:length(files)) {
      f_nm <- sub(".rdata$", "", basename(files[i])) #get filename without extension
      e <- new.env() # initialise a new envir to save objects so don't override
      obj_nm <- load(files[i], envir = e) # get object names
      load(files[i], envir = e) # save objects from rdata into new env
      obj <- mget(obj_nm, envir = e, inherits = FALSE) #save objects from new env into list
      names(obj) <- paste(f_nm, names(obj), sep = "_") # change names as desired 
      list2env(obj, globalenv()) # save into global environment
    }
    
    rm(i, f_nm, e, obj_nm, obj) #clean up global env
    
    

    Hope this helps.