views:

1441

answers:

4

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
setwd("C:/Temp/")
packages <- installed.packages()[,"Package"]
save(packages, file="Rpackages")

Followed by this in the new version:

#--run in the new version
setwd("C:/Temp/")
load("Rpackages")
for (p in setdiff(packages, installed.packages()[,"Package"]))
install.packages(p)
+6  A: 

Two quick suggestions:

  1. Use Gabor's batchfiles which are said to comprise tools helping with e.g. this bulk library relocations. Caveat: I have not used them.

  2. Don't install libraries within the 'filetree' of the installed R version. On Windows, I may put R into C:/opt/R/R-$version but place all libraries into C:/opt/R/library/ using the following snippet as it alleviates the problem in the first place:

    $ cat .Renviron # this is using MSys/MinGW which looks like Cygwin
    ## Example .Renviron on Windows
    R_LIBS="C:/opt/R/library"

Dirk Eddelbuettel
I wonder if transferring packages from, say, R 2.8 to R.9 causes any problems? Or will everything be fine as long as you do a update.packages in the new version?
Eduardo Leoni
I have been doing this for quite a while and have not had problems. R is typically "forward compatible". And IIRC only one upgrade (may have been R 1.9 -> R 2.0) required a rebuild of all libraries.
Dirk Eddelbuettel
That's very good to know. Thanks!
Eduardo Leoni
I also usually just copy my Library folder to my new installation and run update.packages. It seems to work fine. An optional install folder however is much more elegant.
kpierce8
+2  A: 

The method suggested above will not completely work if you have packages that are not from CRAN. For example, a personal package or a package downloaded from a non-CRAN site.

My preferred method on Windows (upgrading 2.10.1 to 2.11.0):

Install R-2.11.0
Copy R-2.10.0/library/* to R-2.11.0/library/
Answer "no" to the prompts asking you if it is okay to overwrite.
Start R 2.11.0 and then type
update.packages()
kevin
Or `update.packages(checkBuilt=TRUE)`
Marek
Or `update.packages(checkBuilt=TRUE, ask=FALSE)` :-P
gd047
+1  A: 

Following Dirk's suggestion, here is some R code to do it on windows:

http://www.r-statistics.com/2010/04/changing-your-r-upgrading-strategy-and-the-r-code-to-do-it-on-windows/

Tal Galili
+3  A: 

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.

install.packages("thepackage",lib="/path/to/directory/with/libraries")

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

.libPaths("/path/to/directory/with/libraries")

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 Rprofile.site 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 Rprofile.site 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.

library(utils)

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

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

.Last <- function(){
  pkgs <- installed.packages()[,1]
  if (length(pkgs) > length(installed)){
    save(pkgs,file="G:\Setinfo\R\packagelist.RData")
  }
}

When I install a new R version, I just add the necessary elements to the Rprofile.site file and all packages are reinstalled. I have to adjust the Rprofile.site 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.

Joris Meys
+1 Great answer.
Shane