Updating R and RStudio and avoiding dependency hell of updating to new major version
Updating R (for Windows)
The most straightforward way to update R on Windows is to use the installr
package. installr
can also be used to install also ‘Rtools’, ‘RStudio’ and ‘Git’.
Keep in mind that it is not recommended to use installr
from RStudio, so use R instead. Some more details on updating R with installr
can be found here.
## Update R for Windows - best from R, do not use RStudio
# Load or, if necessary, install & load {installr} package
if(!require(installr)) {
install.packages("installr");
require(installr)
}
After installing/loading installr
you should chose your preferred method for updating: non-interactive or interactive.
For non-interactive full update sequence use:
To non-interactively install, move packages from old version to new version, update packages and quit R run this:
# For non-interactive full update sequence use:
installr::updateR(browse_news = F,
install_R = T,
copy_packages = T,
copy_site_files = F,
keep_old_packages = F,
update_packages = T,
start_new_R = F,
quit_R = T
) # install, move, update.package, quit R.
For interactive pop-up guided full update sequence use:
This will start the updating process of your R installation. It will check for newer versions, and if one is available, will guide you through the decisions you’d need to make. Once the installation is done, you should press “any-key”, and the function will proceed with copying all of your packages from your old (well, current) R installation, into your newer R installation.
# For interactive pop-up guided full update sequence use:
installr::updateR()
Sequential steps by component functions
You can also run each step of the sequence by calling the component functions of the installr::updateR()
function:
installr::check.for.updates.R() # tells you if there is a new version of R or not.
installr::install.R() # download and run the latest R installer
installr::copy.packages.between.libraries() # copy your packages to the newest R installation from the one version before it (if ask=T, it will ask you between which two versions to perform the copying)
Update RStudio
For interactive install from inside RStudio
Open RStudio.
Navigate to Help Menu.
Check for updates.
If any new update is available, click on Install.
For non-interactive install from R
We can use installr
package to install the new RStudio.
# Install RStudio
installr::install.RStudio()
Updating Packages
If you followed the steps above, you would have moved (i.e. cut/paste) the packages from older version to new version by using installr
. Now, if you transition beween minor version of R, you just need to update the packages:
# Update packages
update.packages(ask = FALSE) # update all packages except those installed from other sources (e.g. with devtools::install_github())
Unfortunately, if you are updating between major version (e.g. R version 3.0 to R version 4.0) updating won’t help because you will get the infamous error:
Package ‘XXX’ was installed before R 4.0.0: please re-install it
This means that you need to re-install all of your packages, which may take some time. I will share the sequence I use, but keep in mind that it uses install.packages()
and is sub-optimal because some packages that are frequent dependencies of others get installed more than once (a single version will remain though).
- You should know where your packages live
# Paths to installed packages
.libPaths()
- Detach all non-core packages
You can detach packages from console or RStudio, but the easiest and safest way is to use pacman
.
# Restart R
.rs.restartR()
# Clear workspace
rm(list = ls(all = TRUE))
# Detach all packages
# Load or, if necessary, install & load {pacman} package
if(!require(pacman)) {
install.packages("pacman");
require(pacman)
}
pacman::p_unload(pacman::p_loaded(), character.only = TRUE)
If needed there is a package called nothing
that when loaded will unload all of the loaded packages. You can install it from GitHub and use it like this:
devtools::install_github("romainfrancois/nothing")
require(nothing, quietly = TRUE)
loadedNamespaces() # only package {base} should remain
- Check what packages remain loaded
If you followed along, only core packages should remain loaded.
# Check what packages remain loaded
loadedNamespaces()
- Re-install all previously installed packages
This will take a while …
# Re-install packages
to_install <- unname(installed.packages()[, "Package"])
for(lib in to_install) install.packages(lib, dependencies = TRUE)