Learn how to do a painless upgrade of your R version
Author
Fabrício Almeida-Silva
Published
May 6, 2022
Motivation
Have you ever upgraded R and lost all of your packages? As a consequence, you had to install them again one by one. One. By. One. Oh, man… Boring, huh? Here, I will guide you on how to upgrade your R version and reinstall your packages automatically. This way, you can spend your time on what really matters: writing some cool R code! This post is inspired by this Gist code.
‘Taking a picture’ of your current R package universe
The first thing you need to do before upgrading your R version is to save a list of all packages you have installed. Not only must you have package names, but also from where they were downloaded (e.g., CRAN, Bioconductor, GitHub, etc.). The code below will create a data frame of packages and their sources, and save it as a .csv file in your current working directory.
NOTE: You need to have the packages tidyverse and sessioninfo installed.
#----Create a data frame with all installed packages and their sources---------library(tidyverse)all_pkg <- sessioninfo::session_info("installed") |>pluck("packages") |>as_tibble()# Classify sources: CRAN, Bioconductor, GitHub, r-universe, and localsplit_repo <- all_pkg |>mutate(repo =case_when(str_detect(source, "Bioconductor") ~"Bioconductor",str_detect(source, "CRAN") ~"CRAN",str_detect(source, "Github") ~"GitHub",str_detect(source, "local") ~"local",str_detect(source, "r-universe") ~"r-universe",TRUE~NA_character_ ), .before ="source") |>select(package, repo)head(split_repo)
You can then export this data frame to a .csv file as follows:
split_repo |>write_csv("packages.csv")
Now that you have a list of all your packages and their sources, you can install the latest version of R. That will vary according to the operating system you use, so you’d better go to the CRAN page and see the instructions on how to upgrade R for your case. In my case, on a Ubuntu 20.04 LTS machine, I just ran:
sudo apt-get updatesudo apt-get upgrade
Once you’re done upgrading your R version, open a new R session (now with the latest version) and run the following code to install all your beloved packages:
For GitHub packages, I suggest looking at them one by one (there shouldn’t be many of them) and deciding which ones you want to install, as many of them are usually unstable and installed for testing purposes. Once you have identified which ones you want to install, you can do it with remotes::install_github().
And that’s all! Whenever you need to upgrade R, just run the same code again and you’re all set. I hope this post helped you!