Migrating Libraries in R (process credit to RBloggers)

Lara Southard, PhD
3 min readJul 1, 2021

I came across a really cool 3-step way to install all your old libraries in a new instance of R (be it a new device or if you update R) here, but I also have come across a few snags when doing this so I’m going to write out my whole process.

Step 0: Prerequisites


Before you migrate libraries, make sure devtoolsis installed. This is done the usual way for installing packages in R using the install.packages() function.


Next, make sure Rtools is installed on your device. I use windows, but directions can be found online here. Make sure (at least for windows), you’re putting Rtools on the PATH when you’re done with the installer and that it’s in the right folder. If you’re using RTools 4.20 you won’t have to do the extra PATHsteps anymore!

Note: you have to install devtools before Rtools and make sure you’re saving Rtools in the file path shown on the installation guide.

Next, I’m following the guide in the link above.

Step 1: Save a list of your packages

Here you want to save all of your packages using the function installed.packages() and write it to a file (in this case a .csv) so you can bring it into your new environment/device.

installed <- as.data.frame(installed.packages()) write.csv(installed, ‘installed_previously.csv’)

End this R session/environment or move to your new device.

Step 2: Create a list of libraries already loaded in your new R session or device

Open R/RStudio on your new device or the updated version of R.

Run the same command as above, but call it something different so you can differentiate it from the previous file:

baseR <- as.data.frame(installed.packages())

Then, bring in the previous file:

installedPreviously <- read.csv(‘installed_previously.csv’)

Step 3: Find any non-duplicated libraries

The RBloggers post uses setdiff() which is effectively an anti_join() but you likely only have baseRpackages installed (so no anti_join()). I like this approach because it’s quick and easy:

toInstall <- setdiff(installedPreviously, baseR)

Now, toInstall is a compilation of all new libraries, or ones you don’t have installed. Make sure there are items in this df.

Step 4: Make toInstall a vector

I’m going to jump ahead for just a minute. Step 5 is going to use the install.packages() function and only takes characters (objects in quotes) or vectors. The RBloggers post immediately puts toInstall into that function and I’ve not been able to get this to work without making it a vector first.

final.installlist<- as.vector(unlist(toInstall$save.pkg.list))

Above, I’ve created the final list (final.installlist) a vector using the first column of toInstall titled save.pkg.list. You may need to check to make sure this is the same naming convention (theoretically it should be), but it’s effectively the column that has all the names of the packages (i.e. column 1, row 1 had just the word tidyverse).

Step 5: Install the list of packages

Now install.packages() will take final.installlist as an argument and you can run this code below. It’ll immediately start installing all your packages!


Other helpful links for snags:



Lara Southard, PhD

trained neuroscientist | professional research scientist | lifelong feminist