Site icon R-bloggers

Updating R

[This article was first published on Fellgernon Bit - rstats, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

As you might know by now, the latest R version was recently released (R 3.4.0). That means that you are highly encouraged to update your R installation. There are several ways to do this some of which are documented in these other blog posts: Tal Galili, 2013, Kris Eberwein, 2015. You would think that it’s just a matter of downloading the latest R installer for your OS, installing it, and continuing your analysis. The reality is a bit more complicated. The following short steps will make your life easier.

Save your list of currently installed packages

If you want to continue using R with all the packages you previously had installed, the best way is to save the list of packages you currently have before installing a new R version. You can do so with these lines of code:

## Change accordingly
list_dir <- '/Users/lcollado/Dropbox/Computing/R'

## Get the list of installed packages
installed <- dir(.libPaths())

## Save the list for later use
save(installed, file = file.path(list_dir, paste0(Sys.Date(), '-installed.Rdata')))

## Explore the list
head(installed)

## [1] "abind"   "acepack" "ada"     "AER"     "affy"    "affyio"

length(installed)

## [1] 611

Install latest R

Ok, now you have a list of installed packages. It’s like a restore point. Next, you need to get the latest R installer for your OS from CRAN and install the latest R. For example, for a Mac that would be R-3.4.0.pkg. Install it as usual.

Restore your packages

By now you have a new R version installed but without all your favorite packages. So, how do you install them? You just need to open your latest list of installed packages and install them. Now, I’m a Bioconductor user which means that some of my packages are not on CRAN. But the following code will work for you even if all the packages you use are from CRAN.

## Change accordingly
list_dir <- '/Users/lcollado/Dropbox/Computing/R'

## Find the corresponding Rdata files
previous <- dir(path = list_dir, pattern = 'installed.Rdata')

## Load the latest one
load(file.path(list_dir, previous[length(previous)]))

## Just checking it
head(installed)

## [1] "abind"   "acepack" "ada"     "AER"     "affy"    "affyio"

Next, get the list of current R packages you have installed. Every new R installation comes with a few of them (the base packages). You don’t need to install those.

current <- dir(.libPaths())

Finally, install the missing packages

## For Bioconductor and CRAN packages
source('http://bioconductor.org/biocLite.R')
biocLite(installed[!installed %in% current])


## Bioconductor version 3.6 (BiocInstaller 1.27.2), ?biocLite for help

and now you can continue on with your analysis ? You didn’t even need to figure out the best order to install the packages!

GitHub packages

Some of your favorite R packages might only exist via GitHub. This list is likely short since most packages get distributed via CRAN. But if that’s the case, you can see which packages are missing by running:

## Check which packages are missing
current_post_installation <- dir(.libPaths())
installed[!installed %in% current_post_installation]

For example, in my case I use the colorout package which lives only in GitHub. I have to install that one manually:

install.packages('devtools')
library('devtools')
install_github("jalvesaq/colorout")

Other times a package might not be compiling for the new R version or might no longer be supported (defunct).

Misc for Bioconductor developers

If you are a Bioconductor developer or are planning on becoming one, then you need 2 versions of R at all times. One R for the bioc-release branch and another one for the bioc-devel branch. Sometimes it’s the same R version sometimes it’s not depending on the month of the year. Right now, Bioc-release (3.5) uses R 3.4.0 and Bioc-devel (3.6) also uses R 3.4.0. R Switch for Mac users will be your friend. I can’t find the old bioc-devel mailing list thread where I first learned this, but the idea is to download the latest R tar ball, change the name from 3.4 to something else (3.4devel in my case), put it back together into a tar ball and then use this tar ball to install a second R version.

## Download latest R tarball
wget http://r.research.att.com/el-capitan/R-3.4-branch/R-3.4-branch-el-capitan-sa-x86_64.tar.gz

## Un-tar it
tar -xvf R-3.4-branch-el-capitan-sa-x86_64.tar.gz

## Renamed files from 3.4 to 3.4 devel
mv Library/Frameworks/R.framework/Versions/3.4 Library/Frameworks/R.framework/Versions/3.4devel

## Put it back in a tar ball
tar -cvzf Rlib.tgz Library

## Install it
sudo tar fvxz Rlib.tgz -C /

There you go:

Reproducibility

## Reproducibility info
library('devtools')
options(width = 120)
session_info()

## Session info -----------------------------------------------------------------------------------------------------------

##  setting  value                       
##  version  R version 3.4.0 (2017-04-21)
##  system   x86_64, darwin15.6.0        
##  ui       X11                         
##  language (EN)                        
##  collate  en_US.UTF-8                 
##  tz       America/New_York            
##  date     2017-05-04

## Packages ---------------------------------------------------------------------------------------------------------------

##  package       * version date       source        
##  BiocInstaller * 1.27.2  2017-05-04 Bioconductor  
##  devtools      * 1.12.0  2016-12-05 CRAN (R 3.4.0)
##  digest          0.6.12  2017-01-27 CRAN (R 3.4.0)
##  evaluate        0.10    2016-10-11 cran (@0.10)  
##  knitr         * 1.15.1  2016-11-22 cran (@1.15.1)
##  magrittr        1.5     2014-11-22 cran (@1.5)   
##  memoise         1.1.0   2017-04-21 CRAN (R 3.4.0)
##  stringi         1.1.5   2017-04-07 cran (@1.1.5) 
##  stringr         1.2.0   2017-02-18 cran (@1.2.0) 
##  withr           1.0.2   2016-06-20 CRAN (R 3.4.0)

Want more?

Check other @jhubiostat student and alumni blogs at Bmore Biostats as well as topics on #rstats.

To leave a comment for the author, please follow the link and comment on their blog: Fellgernon Bit - rstats.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.