Thursday, June 28, 2012

Updating R but keeping your installed packages

This is probably an issue that has been addressed by many blog posts (including these ones: [link1] and [link2]), and can be deduced from R Installation and Administration manual. However, I will post it here for future reference. The problem is that when you update R you usually need to re-install your libraries or change .libPaths() to point to a location that has your previous libraries.

The solution below will work for unix-like operating systems including Mac OS X.

First, we need a location to install all our packages from now on. This can be any directory, and location of this directory should be indicated in ~/.Renviron file. Let's create that directory now:

mkdir ~/Rlibs

We created Rlibs directory in our home directory. Now, create the .Renviron file in your home directory and enter the following line and save the .Renviron file:

 R_LIBS=~/Rlibs

We can now start R and install any library. The libraries will be installed to ~/Rlibs, and when we update R, R will still look for libraries in ~/Rlibs directory so we don't need to re-install the libraries. However, we will need to update the libraries in ~/Rlibs directory to their most recent versions. All we need to do is to run update.packages() in R console, and the libraries will be updated.

You can also update Bioconductor package as follows (this probably works with newer versions of Bioconductor):

source("http://bioconductor.org/biocLite.R")
biocLite("BiocUpgrade")



EDIT:
see here for setting environment variables  in Windows
http://cran.r-project.org/bin/windows/base/rw-FAQ.html#How-do-I-set-environment-variables_003f 

13 comments:

  1. Thank you very much. This is very useful advice indeed!

    ReplyDelete
  2. What operating system are you using and how are you updating R? I use Ubuntu and just update R from the Ubuntu CRAN repository or the RutteR Launchpad PPA and I never have an issue with having to reload packages.

    ReplyDelete
  3. I use Mac OS X or RHEL 5 which I don't have root access. It is possible there are easier ways to update R on those systems, but this is what I'm using at the moment.

    ReplyDelete
  4. Thanks. Something that has frustrated me in the past but given it's an irregular occurence I haven't hunted out a solution. Will set this up :)

    ReplyDelete
  5. is there also a solution for Windows?

    ReplyDelete
    Replies
    1. After installing a new version of R on Windows, I just copy the "library" directory of the old R installation and overwrite the "library" directory of the new R installtion.
      It's always worked for me...

      Ravi

      Delete
  6. check this for a windows solution
    http://cran.r-project.org/bin/windows/base/rw-FAQ.html#How-do-I-set-environment-variables_003f

    ReplyDelete
  7. Hi l2n,

    I've tried this, and I get:

    > .libPaths()
    [1] "/Library/Frameworks/R.framework/Rlib" "/Library/Frameworks/R.framework/Versions/2.15/Resources/library"
    [3] "/Applications/RStudio.app/Contents/Resources/R/library"

    With [1] being the directory I made. However running update.packages() gives me a bunch of these:

    Warning: package 'MASS' in library '/Library/Frameworks/R.framework/Versions/2.15/Resources/library' will not be updated

    So it's not updating the packages in the other libraries. Have you come across this?

    ReplyDelete
    Replies
    1. Hi nzcoops,
      I just tried this and I have core packages installed in /Library/Frameworks/R.framework/Versions/2.15/Resources/library and it worked. I had version 7.3-18 of MASS package installed in that location, now I have version 7.3-19. Didn't get a warning during that process. My .libPaths() show:
      [1] "/Users/l2n/RLibs"
      [2] "/Library/Frameworks/R.framework/Versions/2.15/Resources/library"

      Delete
  8. Cheers. I don't fully understand this, but it seems update.packages() updates packages at all locations in .libPaths(), but install.packages() only installs (by default) to the first element of .libPaths(). I removed the 7 packages giving that error from my .../2.15/Resources/library and I no longer get the warning. Still, doesn't really explain why I (/R) wasn't able to / didn't want to update packages in the second tier library directory *shrug*

    ReplyDelete
  9. what I do on my linux box, is to start R as root, and install any package or update all packages from console. It works like a charm.

    ReplyDelete
  10. Is there a way to install methylKit with the latest R version 3?
    THANKS IN ADVANCE!

    ReplyDelete
  11. Sometime this week, you will be able to use methylKit with R 3.0

    ReplyDelete