Upgrading R version with all your packages

rstats
project management
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 local
split_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)
# A tibble: 6 × 2
  package          repo        
  <chr>            <chr>       
1 abind            CRAN        
2 ade4             CRAN        
3 annotate         Bioconductor
4 AnnotationDbi    Bioconductor
5 AnnotationFilter Bioconductor
6 ape              CRAN        

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 update
sudo 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:

#----First, install tidyverse, remotes, and BiocManager-------------------------
if(!require("tidyverse", quietly = TRUE))
    install.packages("tidyverse")

if(!require("remotes", quietly = TRUE))
    install.packages("remotes")

if(!require("BiocManager", quietly = TRUE))
    install.packages("BiocManager")

#----Read data frame with package names and sources-----------------------------
library(tidyverse)
all_pkg <- readr::read_csv("packages.csv", show_col_types = FALSE)

#----Reinstall packages---------------------------------------------------------
## CRAN packages
cran_pkg <- all_pkg |> 
  dplyr::filter(repo == "CRAN") |> 
  dplyr::pull(package)

cran_pkg |>
  install.packages()


## Bioconductor packages
bioc_pkg <- all_pkg |>
    dplyr::filter(repo == "Bioconductor") |>
    dplyr::pull(package)

bioc_pkg |>
    BiocManager::install()


## R-Universe
runi_pkg <- all_pkg |>
    dplyr::filter(repo == "r-universe") |>
    dplyr::pull(package)

runi_pkg |>
    install.packages(repos = "https://ropensci.r-universe.dev")


## GitHub packages - only list packages
gh_pkg <- all_pkg |>
    dplyr::filter(repo == "GitHub") |>
    dplyr::pull(package)

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!

Session information

sessioninfo::session_info()
─ Session info ───────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.3.0 (2023-04-21)
 os       Ubuntu 20.04.5 LTS
 system   x86_64, linux-gnu
 ui       X11
 language (EN)
 collate  en_US.UTF-8
 ctype    en_US.UTF-8
 tz       Europe/Brussels
 date     2023-06-12
 pandoc   3.1.1 @ /usr/lib/rstudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)

─ Packages ───────────────────────────────────────────────────────────────────
 package     * version date (UTC) lib source
 bit           4.0.5   2022-11-15 [1] CRAN (R 4.3.0)
 bit64         4.0.5   2020-08-30 [1] CRAN (R 4.3.0)
 cli           3.6.1   2023-03-23 [1] CRAN (R 4.3.0)
 colorspace    2.1-0   2023-01-23 [1] CRAN (R 4.3.0)
 crayon        1.5.2   2022-09-29 [1] CRAN (R 4.3.0)
 digest        0.6.31  2022-12-11 [1] CRAN (R 4.3.0)
 dplyr       * 1.1.2   2023-04-20 [1] CRAN (R 4.3.0)
 evaluate      0.20    2023-01-17 [1] CRAN (R 4.3.0)
 fansi         1.0.4   2023-01-22 [1] CRAN (R 4.3.0)
 fastmap       1.1.1   2023-02-24 [1] CRAN (R 4.3.0)
 forcats     * 1.0.0   2023-01-29 [1] CRAN (R 4.3.0)
 generics      0.1.3   2022-07-05 [1] CRAN (R 4.3.0)
 ggplot2     * 3.4.2   2023-04-03 [1] CRAN (R 4.3.0)
 glue          1.6.2   2022-02-24 [1] CRAN (R 4.3.0)
 gtable        0.3.3   2023-03-21 [1] CRAN (R 4.3.0)
 hms           1.1.3   2023-03-21 [1] CRAN (R 4.3.0)
 htmltools     0.5.5   2023-03-23 [1] CRAN (R 4.3.0)
 htmlwidgets   1.6.2   2023-03-17 [1] CRAN (R 4.3.0)
 jsonlite      1.8.4   2022-12-06 [1] CRAN (R 4.3.0)
 knitr         1.42    2023-01-25 [1] CRAN (R 4.3.0)
 lifecycle     1.0.3   2022-10-07 [1] CRAN (R 4.3.0)
 lubridate   * 1.9.2   2023-02-10 [1] CRAN (R 4.3.0)
 magrittr      2.0.3   2022-03-30 [1] CRAN (R 4.3.0)
 munsell       0.5.0   2018-06-12 [1] CRAN (R 4.3.0)
 pillar        1.9.0   2023-03-22 [1] CRAN (R 4.3.0)
 pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.3.0)
 purrr       * 1.0.1   2023-01-10 [1] CRAN (R 4.3.0)
 R6            2.5.1   2021-08-19 [1] CRAN (R 4.3.0)
 readr       * 2.1.4   2023-02-10 [1] CRAN (R 4.3.0)
 rlang         1.1.1   2023-04-28 [1] CRAN (R 4.3.0)
 rmarkdown     2.21    2023-03-26 [1] CRAN (R 4.3.0)
 rstudioapi    0.14    2022-08-22 [1] CRAN (R 4.3.0)
 scales        1.2.1   2022-08-20 [1] CRAN (R 4.3.0)
 sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.3.0)
 stringi       1.7.12  2023-01-11 [1] CRAN (R 4.3.0)
 stringr     * 1.5.0   2022-12-02 [1] CRAN (R 4.3.0)
 tibble      * 3.2.1   2023-03-20 [1] CRAN (R 4.3.0)
 tidyr       * 1.3.0   2023-01-24 [1] CRAN (R 4.3.0)
 tidyselect    1.2.0   2022-10-10 [1] CRAN (R 4.3.0)
 tidyverse   * 2.0.0   2023-02-22 [1] CRAN (R 4.3.0)
 timechange    0.2.0   2023-01-11 [1] CRAN (R 4.3.0)
 tzdb          0.3.0   2022-03-28 [1] CRAN (R 4.3.0)
 utf8          1.2.3   2023-01-31 [1] CRAN (R 4.3.0)
 vctrs         0.6.2   2023-04-19 [1] CRAN (R 4.3.0)
 vroom         1.6.3   2023-04-28 [1] CRAN (R 4.3.0)
 withr         2.5.0   2022-03-03 [1] CRAN (R 4.3.0)
 xfun          0.39    2023-04-20 [1] CRAN (R 4.3.0)
 yaml          2.3.7   2023-01-23 [1] CRAN (R 4.3.0)

 [1] /home/faalm/R/x86_64-pc-linux-gnu-library/4.3
 [2] /usr/local/lib/R/site-library
 [3] /usr/lib/R/site-library
 [4] /usr/lib/R/library

──────────────────────────────────────────────────────────────────────────────