How to Upgrade R Without Losing Your Packages

Share

Yup kids, it’s that time again. The new version of R was just released. In the past I’ve hesitated to upgrade my R version because I knew I would lose all of my packages during the new install, which makes me very grumpy.

I found this neat little trick to save my current packages before the new install and re-load them into the new version. I did this on Mac Yosemite but I this should work on Windows or Linux as well.

1. Before you upgrade, build a temp file with all of your old packages.

tmp <- installed.packages()
installedpkgs <- as.vector(tmp[is.na(tmp[,"Priority"]), 1])
save(installedpkgs, file="installed_old.rda")

2. Install the new version of R and let it do it’s thing.

3. Once you’ve got the new version up and running, reload the saved packages and re-install them from CRAN.

load("installed_old.rda")
tmp <- installed.packages()
installedpkgs.new <- as.vector(tmp[is.na(tmp[,"Priority"]), 1])
missing <- setdiff(installedpkgs, installedpkgs.new)
install.packages(missing)
update.packages()

Note: If you had any packages from BioConductor, you can update those too!

chooseBioCmirror()
biocLite() 
load("installed_old.rda")
tmp <- installed.packages()
installedpkgs.new <- as.vector(tmp[is.na(tmp[,"Priority"]), 1])
missing <- setdiff(installedpkgs, installedpkgs.new)
for (i in 1:length(missing)) biocLite(missing[i])

All done, now you can get back to cracking out R code. This method helped me save a lot of time, hope someone else finds it useful!

15 thoughts on “How to Upgrade R Without Losing Your Packages

  1. everything fine until the third block of code, trying to update Bioconductor packages. Only after running biocLite(“BiocUpgrade”) as a sort of variation of the 3rd line, was I able to get the system of Bioconductor up and running. Wise advice in http://bioconductor.org/install/

    1. Thanks a lot. The Bioconductor piece was based on an old version or R and Bioconductor. Good looking out on a quick fix, I’ll add it to the post:)

  2. “` r
    `installr::updater` from inside RStudio worked for me. And could keep old packages following the installation prompts.
    “`

Leave a Reply