Shane Shane - 2 months ago 5x
R Question

Painless way to install a new version of R?

Andrew Gelman recently lamented the lack of an easy upgrade process for R (probably more relevant on Windows that Linux). Does anyone have a good trick for doing the upgrade, from installing the software to copying all the settings/packages over?

This suggestion was contained in the comments and is what I've been using recently. First you install the new version, then run this in the old verion:

#--run in the old version of R
packages <- installed.packages()[,"Package"]
save(packages, file="Rpackages")

Followed by this in the new version:

#--run in the new version
for (p in setdiff(packages, installed.packages()[,"Package"]))


Just for completeness, there are some ways to prevent you from having this problem. As Dirk said, save your packages in another directory on your computer.


You can change the default .Library value using the function .libPaths too


This will put this path as a first value in the .Library variable, and will make it the default.

If you want to automate this further, you can specify this in the file, which you find in the /etc/ directory of your R build. Then it will load automatically every time R loads, and you don't have to worry about that any more. You can just install and load packages from the specified directory.

Finally, I have some small code included in my allowing me to reinstall all packages when I install a new R version. You just have to list them up before you update to the new R version. I do that using an .RData file containing an updated list with all packages.


## Check necessary packages
load("G:\Setinfo\R\packagelist.RData") # includes a vector "pkgs"
installed <- pkgs %in% installed.packages()[, 'Package']
if (length(pkgs[!installed]) >=1){

I make the packagelist.RData by specifying .Last() in my This updates the package list if I installed some :

.Last <- function(){
  pkgs <- installed.packages()[,1]
  if (length(pkgs) > length(installed)){

When I install a new R version, I just add the necessary elements to the file and all packages are reinstalled. I have to adjust the anyway (using sum contrasts, adding the extra code for Tinn-R, these things), so it's not really extra work. It just takes extra time installing all packages anew.

This last bit is equivalent to what is given in the original question as a solution. I just don't need to worry about getting the "installed" list first.

Again, this doesn't work flawless if you have packages that are not installed from CRAN. But this code is easily extendible to include those ones too.

Edit: There was a missing parenthesis in the code