Creating a Shared Library of R Packages

Goal: Demonstrate how to use an R library to create a shared set of R packages.



Use Case

Consider the following use cases:

  • You’re collaborating on some research and want users of the project to use the same R environment and set/version of a collection of R packages.

  • You’re leading a workshop and want all attendees to learn using the same environment.

In both cases we can setup an R library within a shared location, such as a project folder, which all users can access, and thus use the same set of packages.


General Process

The general process for this is:

  1. Create a folder in a shared location.

  2. Load and start R.

  3. Update the library paths to point to this location.

  4. Install R packages.

Every time this is to be used:

  1. Load and start R.

  2. Update the library paths to point to this location.


Example

# Create R Library folder: []$ cd /project/arcc/software/ []$ mkdir -p r_library/r_workshop []$ cd r_library/r_workshop/ []$ pwd /project/<project-name>/software/r_library/r_workshop # Load and Start R []$ module purge []$ module load gcc/14.2.0 r/4.4.0 []$ R # Check current library paths: > .libPaths() [1] "/cluster/medbow/home/<username>/R/x86_64-pc-linux-gnu-library/4.4" [2] "/apps/u/spack/gcc/14.2.0/r/4.4.0-w7xoohc/rlib/R/library"

Current Available R Packages

> write.table(installed.packages()[,c(1,2,3:4)]) "Package" "LibPath" "Version" "Priority" "class" "class" "/cluster/medbow/home/<username>/R/x86_64-pc-linux-gnu-library/4.4" "7.3-22" "recommended" ... "XML" "XML" "/cluster/medbow/home/<username>/R/x86_64-pc-linux-gnu-library/4.4" "3.99-0.17" NA "base" "base" "/apps/u/spack/gcc/14.2.0/r/4.4.0-w7xoohc/rlib/R/library" "4.4.0" "base" ... "utils" "utils" "/apps/u/spack/gcc/14.2.0/r/4.4.0-w7xoohc/rlib/R/library" "4.4.0" "base"

Note: We can current see packages installed:

  1. Under our home folder.

  2. Base packages installed as part of this R version.


Update the Library Path

> .libPaths(c('/project/<project-name>/software/r_library/r_workshop', '/apps/u/spack/gcc/12.2.0/r/4.4.0-7i7afpk/rlib/R/library')) > write.table(installed.packages()[,c(1,2,3:4)]) "Package" "LibPath" "Version" "Priority" "base" "base" "/apps/u/spack/gcc/14.2.0/r/4.4.0-w7xoohc/rlib/R/library" "4.4.0" "base" ... "utils" "utils" "/apps/u/spack/gcc/14.2.0/r/4.4.0-w7xoohc/rlib/R/library" "4.4.0" "base"

Install a package into the New Library Location


Test and Use

Error:

Success:


Warning: Remember