Loading multiple RData objects without overwriting your existing objects

About 3 times a year, I forget how to load in multiple RData objects into a session. I always remember load() and I always get confused when an object in my environment changes and something breaks. This is because load() overwrites identically named objects.

Adam Lee Rich has written about this already over at R-bloggers. There’s 1 particular function that’s really helpful to add to your .RProfile:

loadEnvironment <- function(RData, env = new.env()){
    load(RData, env)
    return(env)
}

If you’re like me and repeat object names (e.g. df, db, fit, test, etc.), you can avoid this by using loadEnvironment() and extract a particular object:

env.1 <- loadEnvironment('my.RData')
env.2 <- loadEnvironment('myOther.RData')
myFirstDF <- my.env$df
mySecondDF <- myOther.env$df
Avatar
Wyatt J. McDonnell
American adaptive immunologist

I’m a computational immunologist by training, and found my way into biotechnology as an inventor and technologist along the way. I’m interested in impactful work that improves human health by harnessing human adaptive immunity.