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)

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
Wyatt J. McDonnell
Computational Immunologist

I’m a computational immunologist at 10x Genomics, where I get to work on single-cell technology and the adaptive immune system.