2 minute read

Introduction

Re-installing packages after an R update always seems be a bit of a pain. I can never remember exactly what I did the last time I updated, so I always end up looking through the same Google search results, trying to land on the easiest/most efficient way of making library migration a little easier.

Eventually, I decided that it was time to standardize this process. I wrote an R script, ship_packages.R, that contains two functions. These functions will:

  1. Export information for all of the packages in a given R library to a CSV file or data frame.

  2. Automatically install packages from CRAN, Bioconductor, and/or GitHub based on the information output from the first function.

The script itself is best used when source’d.

Address packages

The first function in the R script is address_packages(). This function uses installed_packages() and packageDescription() to retrieve package information about the packages installed in a given library. The information retrieved includes package name, path, version, dependencies, imports, R build version, and repository (e.g., CRAN, Bioconductor, GitHub, etc.).

Τhe default behavior of address_packages() is to write information about the current library (i.e., before updating) into a CSV file in the current working directory. This can be done by simply running address_packages(). However, the behavior is somewhat flexible.

In the event that you forget to run the function before updating, you can specify a specific library path with the lib.path argument. In this case, you can also avoid writing a CSV file and run the function directly in the new R environment by setting write = FALSE. If you’re retrieving information about a library that is not the current R library, the version of the target library must be provided. The function will check that the provided version and the provided library path are compatible. This is intended to help ensure that the retrieved information is the desired information.

Deliver packages

The second function is deliver_packages(). This function takes as input the output of address_packages(). The pkgs argument in deliver_packages() can accept either a file path (to the CSV file) or a data frame. Package information is then parsed and packages from the defined repositories (currently any/all of CRAN, Bioconductor, and/or GitHub) are installed. Installation from CRAN does not require any exogenous packages. Installation from Bioconductor and GitHub, however, require the installation of BiocManger and devtools, respectively. These packages are automatically installed (if they are not already) if installation from these repositories is requested. If there are packages included in the input information that you do not want installed, define them by name with the omit argument.

Example usage

  • In the current R version (before updating), run address_packages(). The code below will write out a CSV file to the stated directory path. The current R library/version are exported by default.
address_packages( dir.path = "~/path/to/directory" )
  • Update R.

  • In the new R version, run deliver_packages(). By default, the function will install all packages from CRAN, Bioconductor, and GitHub (i.e., no packages are omitted).

deliver_packages( pkgs = "~/path/to/directory/packages_R-4.3.1_2023-10-25.csv" )

Updated: