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
Just download the installer from CRAN and run it. if R is already installed, the new version will be installed without removing any older version but making the last installed version the one used by default. For major version updates such as 3.4.4 to 3.5.0 a new library will be created and the recommended packages installed into it. Once you are sure that you the old version is no longer needed you can uninstall it.
Copying R libraries
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. You may want to edit the vector, or to remove packages no longer needed from the old installation before creating the list of packages to install.
# run in old installation current.pkg.list <- installed.packages()[is.na(installed.packages()[ , "Priority"]), 1] current.pkg.list <- as.character(current.pkg.list) save(current.pkg.list, file = "~/old-pkg-list.rda")
We need then to open the console in the newly installed version of R, read the file with the vector of package names, and install them.
# run in new installation # If you have installed packages from repos other than CRAN # make sure to enable them setRepositories() 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(current.pkg.list, new.pkg.list) install.packages(missing.pkg.list)
After installation we can 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 have been removed from CRAN meanwhile the maintainers update, and 4) some packages were installed locally from files and are not available at the default repositories.
# check what packages are still missing new.pkg.list <- installed.packages()[is.na(installed.packages()[ , "Priority"]), 1] new.pkg.list <- as.character(new.pkg.list) missing.pkg.list <- setdiff(current.pkg.list, new.pkg.list) cat(missing.pkg.list, sep = "\n")
As a final step we may want to clean-up by deleting the file. I suggest not to delete this file immediately as it can still be useful.
As a side note, be aware that the list of packages is just an R character 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
old.pkg.list <- scan("~/old-pkg-list.txt", what = character())
In-between saving and reading one can edit the file in a text editor to remove or and package names.