11 Maintaining R
11.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.
11.1.1 In RStudio
11.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.
11.2 How to upgrade all out-of-date packages
11.2.1 In RStudio
11.2.2 CRAN packages
devtools::update_packages(TRUE)
11.3 How to downgrade a package
First if unsure what version -> CRAN page -> pkgname archive
::install_version("devtools", "1.13.3") devtools
11.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.
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.
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
::dir_create("~/Library/R/3.5/library")
fs
# Re-start R so the .libPaths are updated
# Lookup what packages were in your old package library
<- fs::path_file(fs::dir_ls("~/Library/R/3.4/library"))
pkgs
# Filter these packages as needed
# Install the packages in the new version
install.packages(pkgs)