Upgrading R

Introduction

It is always best to keep R up-to-date. R versions are named with three digits, e.g. 3.3.2. Versions differing in the last digit, sometimes called dot updates, are almost always bug fixes, adding very little new functionality or at least not requiring changes to packages, and not requiring re-installation of packages. Versions with different middle digit, are less frequent, and may require reinstallation of packages. Because of this, library folder names such as 3.3 or 3.4 are used. Consequently, depending on the upgrade a new library folder may be created at the time of installation of a new version of R.

It is important to remember that different versions of R can coexist in the same computer, each using a different library when required. R can be upgraded by simply manually installing the new version without uninstalling earlier versions, which will remain functional. Earlier versions can be uninstalled at a later time, after checking that the new installation works as expected. If this is the case, and you would like to install the same packages you had in the old installation in the new one, or if you would like to install the same packages in another computer, see the instructions given below under Copying  R libraries.

Installing a new version of R

You can very upgrade R with package ‘installr’. These are the steps, that should be carried not in RStudio but in RGUI (e.g. double click the R icon on your desktop in Windows, or even better right click on the icon and select Run as administrator from the drop-down menu).

Then type at the R console:

install.packages("installr") # install
setInternet2(TRUE) # only for R versions older than 3.3.0
installr::updateR() # updating R.

Follow the prompts. I suggest that you copy and update the packages in the library. In some cases you will be able to copy and update the packages in the library using the pop-up menus. If this fails, and you want to install the same set of packages, see the next section.

Alternatively, simply install the new version of R alongside the previously installed one, as a fresh installation following the instructions for your operating system at given in CRAN. After installation copy the R library, as described below.

Copying R libraries

R libraries can the also copied with package ‘installr’.

installr::copy.packages.between.libraries(ask = TRUE)

With ask = TRUE the user  is presented a list of libraries in a simple menu. (You may need to type a space character before the number of the choice, to get the selections accepted.)

The procedure for a manual copy is conceptually very simple. First we save to a file the names of all packages installed in the old R library, and in a second step we use the saved list to install the same packages in another R library. There are a few additional tricks we can use on the way.

The first step is to run at the R console of the old version of R the following commands to create a vector of  character strings with the names of user-installed packages and save the vector to a file on disk.

old.pkg.list <- installed.packages()[is.na(installed.packages()[ , "Priority"]), 1]
old.pkg.list <- as.character(old.pkg.list)
save(old.pkg.list, file = "~/old-pkg-list.rda")

The second step is to load the vector of package names from the file, discard names of packages already installed, and then install the remaining ones.

load(file =  "~/old-pkg-list.rda")
new.pkg.list <- installed.packages()[is.na(installed.packages()[ , "Priority"]), 1]
new.pkg.list <- as.character(new.pkg.list)
missing.pkg.list <- setdiff(old.pkg.list, new.pkg.list)
install.packages(missing.pkg.list)

One can after installation run once more the first four lines of the chunk immediately above, to check if there are any differences remaining.  Possible causes of failure to install packages are: 1) a different set of repositories was selected, 2) some packages had been installed directly from a Git repository using functions from package ‘devtools’, 3) some packages are not compatible with the new version of R, and the maintainers have not yet updated them, and 4) some packages were installed locally from files and are not available at the default repositories.

As a final step we may clean-up by deleting the file.

file.remove("~/old-pkg-list.rda)

 As a side note, be aware that the list of packages is just a character vector that can be manipulated as any other such R vector. Above, we used save() to write the vector to a binary file. Alternatively, the vector can be saved to a text file using cat(). The next chunk shows have to save the names one per line of a text file, by inserting a new line \n as separator.

cat(old.pkg.list, sep = "\n", file = "~/old-pkg-list.txt")

And then read with scan().

old.pkg.list <- scan("~/old-pkg-list.txt", what = character())

In-between we could edit the file in a text editor if needed.