Painless way to install a new version of R?

Viewed 64578

Andrew Gelman recently lamented the lack of an easy upgrade process for R (probably more relevant on Windows than 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)
12 Answers

linux + bash + debian + apt users:

  1. If you're installing/upgrading to the newest version of R, then we may assume you have root permissions. (Not essential, just makes the process a lot simpler; for consistency the script below uses sudo for all installs.) As the R packages are also installed by root, it is thus permissible to place these in /usr/local/.

  2. The call to curl below assumes you are already interested in the sid release of R, the very latest unstable version (as required when building/checking an R package) i.e.

    cat /etc/apt/sources.list | grep 'sid' || exit 1

    although this could easily be replaced with a recent stable release e.g. buster.

  3. Note that I am not using a key as is typically recommended. This is not essential, particularly if (as in the script which follows) we install packages within R itself (Rscript -e below). Also, such keys have a tendency to break/change every few years. Thus, you are of course welcome to add the following preface to the file R.sh which follows:

    sudo apt-key adv --keyserver keyserver.ubuntu.com \ --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9

  4. The array of R packages is clearly not exhaustive but gives some examples which I personally find useful. A fresh install/upgrade with the debian package r-recommended, as below, should give the latest version of all of the the standard 'recommended' packages (e.g. survival). I believe there may be a slight lag between a CRAN release and an update to the relevant debian package. Thus, you may wish to add some of these to the array below if having the latest version of a 'recommended' R package is essential.

  5. The debian packages installed in the process below are also neither essential (for using r-base) nor exhaustive but provide a no. of 'add-ons' which are important for a reasonable no. of R packages.

Anyway... place the following in R.sh:

sudo apt update && sudo apt --yes full-upgrade
sudo apt install --yes libappstream4 curl
### ov1 = online version; lv1 = local version (i.e. currently installed)
ov1=$(curl --silent --url https://packages.debian.org/sid/r-base |
    grep 'meta name=\"Keywords\"' |
    grep --only-matching '[0-9].*[0-9]') ; echo $ov1
## command -v = print a description of COMMAND similar to the `type' builtin
## && = if prior command succeeds, then do; || = if prior fails, then do
command -v 'R --version' &&
    lv1=$(R --version |
              grep --only-matching '[0-9\.]*[0-9]' |
              ## || = otherwise
              head -1) ||
        lv1=0
## 'lt' = less than
if dpkg --compare-versions "$lv1" 'lt' "$ov1" 
then ## declare -a = indexed array
     declare -a deb1=('r-base' 'r-base-dev' 'r-recommended')
     for i in "${deb1[@]}"
     do sudo apt install --yes "$i"
     done
fi
### certain Debian packages are required by 'R' so best have these first
sudo apt install --yes ccache libcairo2-dev libxml2-dev libcurl4-openssl-dev \
     libssl-dev liblapack-dev libssl-dev
declare -a pkg1=('data.table' 'ggplot2' 'knitr' 'devtools' 'roxygen2')
## installing as 'root' so these are installed in
Rscript -e ".libPaths()[1]"
for i in "${pkg1[@]}"
do sudo Rscript -e "install.packages('$i', dependencies=TRUE)"
done
### other useful additions
sudo apt install --yes libblas-dev libboost-dev libarmadillo-dev \
     jags pandoc pandoc-citeproc 
sudo apt update && sudo apt full-upgrade

Then execute it, e.g. assuming in directory already: source R.sh.

Installing packages (whether debian or R) one-by-one in a loop from shell is somewhat inefficient, but allows for simpler tracing of errors, IMHO. May take some time depending on the no. of R packages, so maybe simplest to let run overnight...

In linux, Now it is very simple. Just make:

install.packages("ropenblas")
ropenblas::rcompiler()
Related