12  Maintaining R

12.1 How to upgrade an installed package to the latest version.

Sometimes you would like to upgrade a particular package to the latest available version. Often this is because you have heard about a new feature, or maybe you have run into a bug that may have been fixed.

12.1.1 In RStudio

RStudio has an update dialog (Packages Tab -> Update). Check packages to update them

12.1.2 R terminal

Devtools has a function update_packages() which will upgrade a package (from the same source) for any CRAN or development package.

devtools::update_packages("pkgname")

In addition if the given package is not already installed it will install it.

12.2 How to upgrade all out-of-date packages

12.2.1 In RStudio

RStudio also allows you to update all packages (Packages Tab -> Update -> Select All)

12.2.2 CRAN packages

devtools::update_packages(TRUE)

12.3 How to downgrade a package

First if unsure what version -> CRAN page -> pkgname archive

devtools::install_version("devtools", "1.13.3")

12.4 How to transfer your library when updating R

Often you will not need to do anything when updating R. For ‘patch’ R versions, the ‘z’ in ‘x.y.z’ the R core developers ensure package compatibility across versions. So if you are updating from R 3.5.0 to R 3.5.1 you can use the same packages you are currently using.

For ‘minor’ version changes, the ‘y’ in ‘x.y.z’ the package interface can change, so packages need to be re-installed.

Warning

You may see some suggestions that you can just copy your packages even when the ‘minor’ version changes. DO NOT DO THIS. While it may work some (even most) of the time, R-core does not guarantee compatibility between these versions and things could break (even break silently).

I suggest you keep R’s base and recommended packages separate from the other packages you install from CRAN or elsewhere. This makes it easy to re-install R if needed, without touching your CRAN packages. You also want to make sure the package library is specific to the minor version of R. R_LIBS_USER is actually set by default to this scheme:

  • Windows, R < 4.2: ~/R/win-library/x.y
  • Windows, R >= 4.2: ~/AppData/Local/R/win-library/x.y
  • macOS: ~/Library/R/x.y/library

Note, however, that this directory does not necessarily exist and will not necessarily be created automatically. Therefore, to adopt this lifestyle next time you upgrade R, make sure that this directory exists before you start to re-install your add-on packages (fs::dir_create(Sys.getenv("R_LIBS_USER"))).

You can also alternatively set R_LIBS_USER to a different path; but make sure to include the %v wildcard. e.g. ~/R/library/%v. The %v is automatically expanded to the major and minor version of R, so with R 3.5.1 this path becomes ~/R/library/3.5. See Renviron for how to edit your .Renviron file.

Warning

Paths in R_LIBS_USER are only used if the directories they specify actually exist. So in addition to adding R_LIBS_USER to your .Renviron you need to create the directory as well.

Once this is setup, the process for transferring your package library becomes. (assumes R_LIBS_USER is set to ~/Library/R/3.5/library).

# Install new version of R (lets say 3.5.0 in this example)

# Create a new directory for the version of R
fs::dir_create("~/Library/R/3.5/library")

# Re-start R so the .libPaths are updated

# Lookup what packages were in your old package library
pkgs <- fs::path_file(fs::dir_ls("~/Library/R/3.4/library"))

# Filter these packages as needed

# Install the packages in the new version
install.packages(pkgs)