## Pen & Paper

#### updating R and its installed packages

09 Oct 2015

I have been using a script to automatically reinstate all the installed packages following a R upgrade. This is my preferred method to avoid manually re-installing the packages one by one. The script is simple and works with certainty under Windows or Linux. An alternative is to preserve the existing “library” folder and copy it over the newly created “library” folder after the upgrade is finished. Under Windows, this should not be a problem as by default, there is but one “library”. In Linux, however, the “library” is kept in several locations and may easily become another source of headache. .libPaths() will display the library locations.

There are two simple scripts involved to automate the package re-installation process. Firstly, source the following script before the upgrade to keep track of all the installed packages. The records will be written down in a file packageList.Rdata.

package.list <- installed.packages()[,"Package"]
save(package.list, file="packageList.Rdata")

Secondly, source the following script after the upgrade to load the records. These are compared with what are already present in the newly installed library. The missing packages will be reinstalled.

load("packageList.Rdata")
for (
d in setdiff(package.list, installed.packages()[,"Package"])
)
{
install.package(d)
}