Updating R on Linux - lmmx/devnotes GitHub Wiki

Preliminary notes

  • NB - if on Windows, use Tal Galili's installr (upgrade R from within R!)

  • you may want to save a list (as text file) from the vector of installed.packages() before updating

store_packages.R :

store_packages.R

stores a list of your currently installed packages

tmp.pkg.list <- installed.packages()

installedpackages <- as.vector(tmp.pkg.list[is.na(tmp.pkg.list[,"Priority"]), 1]) save(installedpackages, file="~/Downloads/installed_packages.rda")


* If you forget to do this you can still do it later with the `lib.loc` parameter to `installed.packages()`. It's also better to use the `lib.loc` parameter as you can specify only those packages that are tied to the version of R.
  * `.libPaths()` shows the version-specific path: for me it is `~/R/x86_64-pc-linux-gnu-library/3.*`. Using just the most recent one (since I was working fine without those in pre-3.2 versions), replace the line above with:  
      
    ```R
    tmp.pkg.list <- installed.packages(lib.loc = "~/R/x86_64-pc-linux-gnu-library/3.2")
    ```

- - -

* `sudo vim /etc/apt/sources.list`
* Add `deb https://mirrors.ebi.ac.uk/CRAN/bin/linux/ubuntu trusty/`
* `deb `___`MIRROR_URL`___`/bin/linux/ubuntu `___`LINUX_CODENAME`___`/`  
↳ __`MIRROR_URL`__ : `https://mirrors.ebi.ac.uk/CRAN/` — via [list of mirrors here](https://cran.r-project.org/mirrors.html)  
↳ __`LINUX_CODENAME`__ : `trusty` — via `cat /etc/*-release`
* `sudo apt-get update`

- - -

### Problem: GPG key has expired (R's Secure APT)

* https://mirrors.ebi.ac.uk/CRAN gave:  

GPG error: https://mirrors.ebi.ac.uk trusty/ Release: The following signatures were invalid: KEYEXPIRED 1445181253 KEYEXPIRED 1445181253 KEYEXPIRED 1445181253

...switched to https://www.stats.bris.ac.uk/R/ - same there
* switched off HTTPS using http://mirrors.ebi.ac.uk/CRAN/ and problem persisted

### Solution: add new key [given here](https://cran.r-project.org/bin/linux/ubuntu/README.html#secure-apt)

* [Via notes at CRAN on _Secure APT_](https://cran.r-project.org/bin/linux/ubuntu/README.html#secure-apt) via [this ServerFault question](http://serverfault.com/questions/730316/error-when-running-apt-get-update-on-ubuntu-14)
* run `sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys `___`GPG_KEY`___  
↳ __`GPG_KEY`__ : currently `E084DAB9` (June 2016, apparently changed mid-Oct '15)  
↳ `sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E084DAB9`
- - -

* `sudo apt-get install r-base`

> Users who need to compile R packages from source [e.g. package maintainers, or anyone installing packages with install.packages()] should also install the r-base-dev package:

> ```
sudo apt-get install r-base-dev
  • (Followed the system hint to run sudo apt-get update again after this step)
~ $ R

R version 3.3.0 (2016-05-03) -- "Supposedly Educational"

Upgrade complete :sunglasses:

Reinstalling packages

You could reinstall all packages in the new version of R using this trick

store_packages.R :

# store_packages.R
#
# stores a list of your currently installed packages

tmp.pkg.list <- installed.packages() # I'd advise using the lib.loc parameter, see note above

installedpackages <- as.vector(tmp.pkg.list[is.na(tmp.pkg.list[,"Priority"]), 1])
save(installedpackages, file="~/Downloads/installed_packages.rda")

restore_packages.R :

# restore_packages.R
#
# installs each package from the stored list of packages

load("~/Downloads/installed_packages.rda") # optional if you've already upgraded R

for (count in 1:length(installedpackages)) install.packages(installedpackages[count])

...however this duplication is potentially wasteful of space over time. Instead you could move your packages to a new path which will persist across further upgrades, as recommended here

Here, I'm using ~/opt/R/libs/ as my new personal package library

new.lib.path <- path.expand("~/opt/R/libs/")
.libPaths(new.lib.path)

To retain this setting, edit the Rprofile.site in the R installation's /etc/ directory.

  • find (or confirm) the /etc/ location with whereis R

ls -l /etc/R/Rprofile.site -rw-r--r-- 1 root root 807 Dec 11 2014 /etc/R/Rprofile.site


* `sudo vim /etc/R/Rprofile.site` and add the `.libPaths` line above [as appropriate]


## Finishing the job

This batch install may fail for some along the way, but you'll probably have trouble seeing which in the huge logs, so a second run is useful:

```R
load("~/Downloads/installed_packages.rda")
for (count in 1:length(installedpackages)) {
each.pkg <- installedpackages[count]
if (! each.pkg %in% installed.packages()) {
  install.packages(each.pkg)
}
}

Non-CRAN packages

The install.packages loop should give some warnings and tell you to run warnings() to see them, along the lines of package packagename is not available (for R version ..*).

Bioconductor packages

These will be in the list if you're a biologist, otherwise ignore this section.

> source("https://bioconductor.org/biocLite.R")
Bioconductor version 3.0 (BiocInstaller 1.16.5), ?biocLite for help
BiocInstaller version 3.0 is too old for R version 3.3.0;
...
A new version of Bioconductor is available after installing the most recent
  version of R; see http://bioconductor.org/install

You may have to run sudo R if BiocInstaller is in a root access area, e.g. for me /usr/local/lib/R/site-library/, found via shell command:

function findafile (){ find / -iname "$@" 2>/dev/null; }
findafile BiocInstaller

If installations appear, but these locations are not found by running the R function .libPaths(), then no need to remove them.

# remove.packages("BiocInstaller") didn't work as it looked in the default library for me
remove.packages("BiocInstaller", lib="/usr/local/lib/R/site-library/")
source("http://bioconductor.org/biocLite.R")
biocLite()