/
Where are R Packages Installed on the Cluster?

Where are R Packages Installed on the Cluster?

Goal: Understand where R installs packages and where libraries are located, as well as inspecting general R system configuration.



Terminology Package vs Library

R-Bloggers: Packages v. Libraries in R: Packages are collections of R functions, data, and compiled code in a well-defined format. The directory where packages are stored is called the library:

  • A package is a directory of files which extend R, either a source package (the master files of a package), or a tarball containing the files of a source package, or an installed package, the result of running R CMD INSTALL on a source package. On some platforms there are also binary packages, a zip file or tarball containing the files of an installed package which can be unpacked rather than installing from sources.

  • A package is not a library. The latter is used in two senses in R documentation. The first is a directory into which packages are installed, e.g. /usr/lib/R/library: in that sense it is sometimes referred to as a library directory or library tree (since the library is a directory which contains packages as directories, which themselves contain directories). …


Load an R Environment via the Module System

[]$ module purge []$ module load gcc/14.2.0 r/4.4.0 []$ R --version R version 4.4.0 (2024-04-24) -- "Puppy Cup" ... # Run a simple function. []$ R -e "print(version[['version.string']])" R version 4.4.0 (2024-04-24) -- "Puppy Cup" ... > print(version[['version.string']]) [1] "R version 4.4.0 (2024-04-24)" # Run an R script. []$ Rscript r_test.R [1] "R version 4.4.0 (2024-04-24)"

Inspect R Environment Configuration

[]$ R R version 4.4.0 (2024-04-24) -- "Puppy Cup" ... # List ALL environment variables: > Sys.getenv() # Get single environment variable. > Sys.getenv("R_HOME") [1] "/apps/u/spack/gcc/14.2.0/r/4.4.0-w7xoohc/rlib/R" # Get a list of environment variables. > Sys.getenv(c("R_PLATFORM", "R_HOME", "R_LIBS_USER")) R_PLATFORM "x86_64-pc-linux-gnu" R_HOME "/apps/u/spack/gcc/14.2.0/r/4.4.0-w7xoohc/rlib/R" R_LIBS_USER "/cluster/medbow/home/salexan5/R/x86_64-pc-linux-gnu-library/4.4"

Notice that User Libraries are stored under your home, in: R/x86_64-pc-linux-gnu-library/4.4

In general, under ~/R/<R_PLATFORM>/versionX.Y/


Get R Related Environment variables

Quick and dirty way to list all R_* environment variables.

[salexan5@mblog2 ~]$ R -e "Sys.getenv()" | grep R_ LMOD_FAMILY_COMPILER_VERSION ... R_HOME /apps/u/spack/gcc/14.2.0/r/4.4.0-w7xoohc/rlib/R R_INCLUDE_DIR /apps/u/spack/gcc/14.2.0/r/4.4.0-w7xoohc/rlib/R/include R_LIBS_SITE /apps/u/spack/gcc/14.2.0/r/4.4.0-w7xoohc/rlib/R/site-library R_LIBS_USER /cluster/medbow/home/<username>/R/x86_64-pc-linux-gnu-library/4.4 ... R_PLATFORM x86_64-pc-linux-gnu ... R_SHARE_DIR /apps/u/spack/gcc/14.2.0/r/4.4.0-w7xoohc/rlib/R/share ... R_UNZIPCMD /usr/bin/unzip R_ZIPCMD /usr/bin/zip thunderer_FAMILY_COMPILER_VERSION

Where are Packages Installed

> help(".libPaths") .Library package:base R Documentation Search Paths for Packages Description: ‘.libPaths’ gets/sets the library trees within which packages are looked for.
[]$ R -e ".libPaths()" ... > .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" # Compare against: R_LIBS_USER: /cluster/medbow/home/<username>/R/x86_64-pc-linux-gnu-library/4.4 R_LIBS_SITE: /apps/u/spack/gcc/14.2.0/r/4.4.0-w7xoohc/rlib/R/site-library

/cluster/medbow/home/<username>/R/x86_64-pc-linux-gnu-library/4.4

This is where, for this R platform and version, your packages will be installed.

/apps/u/spack/gcc/14.2.0/r/4.4.0-w7xoohc/rlib/R/library

This is where the base packages for this R platform/version are installed.

You will not have permissions to install into this location.

[salexan5@mblog2 ~]$ ls /apps/u/spack/gcc/14.2.0/r/4.4.0-w7xoohc/rlib/R/library base compiler datasets graphics grDevices grid methods parallel splines stats stats4 tcltk tools translations utils

How to Install Packages (from within R)

# Within R: > help(install.packages) install.packages package:utils R Documentation Install Packages from Repositories or Local Files Description: Download and install packages from CRAN-like repositories or from local files. Usage: install.packages(pkgs, lib, repos = getOption("repos"), contriburl = contrib.url(repos, type), method, available = NULL, destdir = NULL, dependencies = NA, type = getOption("pkgType"), configure.args = getOption("configure.args"), configure.vars = getOption("configure.vars"), clean = FALSE, Ncpus = getOption("Ncpus", 1L), verbose = getOption("verbose"), libs_only = FALSE, INSTALL_opts, quiet = FALSE, keep_outputs = FALSE, ...) ...

How to Install Packages (from outside of R)

[]$ R CMD INSTALL --help Usage: R CMD INSTALL [options] pkgs Install the add-on packages specified by pkgs. The elements of pkgs can be relative or absolute paths to directories with the package sources, or to gzipped package 'tar' archives. The library tree to install to can be specified via '--library'. By default, packages are installed in the library tree rooted at the first directory in .libPaths() for an R session run in the current environment. Options: -h, --help print short help message and exit -v, --version print INSTALL version info and exit -c, --clean remove files created during installation ...

Note: “The elements of pkgs can be relative or absolute paths to directories with the package sources, or to gzipped package 'tar' archives.

i.e. You have the package already downloaded.


Install tidyr Package

# Within R: > install.packages("tidyr") Installing package into ‘/cluster/medbow/home/<username>/R/x86_64-pc-linux-gnu-library/4.4’ (as ‘lib’ is unspecified) --- Please select a CRAN mirror for use in this session --- ... also installing the dependencies ‘utf8’, ‘generics’, ‘pillar’, ‘R6’, ‘stringi’, ‘fansi’, ‘pkgconfig’, ‘withr’, ‘cli’, ‘dplyr’, ‘glue’, ‘lifecycle’, ‘magrittr’, ‘purrr’, ‘rlang’, ‘stringr’, ‘tibble’, ‘tidyselect’, ‘vctrs’, ‘cpp11’ ... * DONE (tidyr)

Lets check:

[salexan5@mblog2 ~]$ ls /home/<username>/R/x86_64-pc-linux-gnu-library/4.4 cli cpp11 dplyr fansi generics glue lifecycle magrittr pillar pkgconfig purrr R6 rlang stringi stringr tibble tidyr tidyselect utf8 vctrs withr

What’s Installed?

# Within R: > write.table(installed.packages()[,c(1,2,3:4)]) "Package" "LibPath" "Version" "Priority" "cli" "cli" "/cluster/medbow/home/<username>/R/x86_64-pc-linux-gnu-library/4.4" "3.6.3" NA "cpp11" "cpp11" "/cluster/medbow/home/<username>/R/x86_64-pc-linux-gnu-library/4.4" "0.4.7" NA ... "tcltk" "tcltk" "/apps/u/spack/gcc/14.2.0/r/4.4.0-w7xoohc/rlib/R/library" "4.4.0" "base" "tools" "tools" "/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 what is identified as base.

> write.table(installed.packages()[,c(1,2,3:4)]) "Package" "LibPath" "Version" "Priority" "cli" "cli" "/cluster/medbow/home/<username>/R/x86_64-pc-linux-gnu-library/4.4" "3.6.3" NA "cpp11" "cpp11" "/cluster/medbow/home/<username>/R/x86_64-pc-linux-gnu-library/4.4" "0.4.7" NA "dplyr" "dplyr" "/cluster/medbow/home/<username>/R/x86_64-pc-linux-gnu-library/4.4" "1.1.4" NA "fansi" "fansi" "/cluster/medbow/home/<username>/R/x86_64-pc-linux-gnu-library/4.4" "1.0.6" NA "generics" "generics" "/cluster/medbow/home/<username>/R/x86_64-pc-linux-gnu-library/4.4" "0.1.3" NA "glue" "glue" "/cluster/medbow/home/<username>/R/x86_64-pc-linux-gnu-library/4.4" "1.7.0" NA "lifecycle" "lifecycle" "/cluster/medbow/home/<username>/R/x86_64-pc-linux-gnu-library/4.4" "1.0.4" NA "magrittr" "magrittr" "/cluster/medbow/home/<username>/R/x86_64-pc-linux-gnu-library/4.4" "2.0.3" NA "pillar" "pillar" "/cluster/medbow/home/<username>/R/x86_64-pc-linux-gnu-library/4.4" "1.9.0" NA "pkgconfig" "pkgconfig" "/cluster/medbow/home/<username>/R/x86_64-pc-linux-gnu-library/4.4" "2.0.3" NA "purrr" "purrr" "/cluster/medbow/home/<username>/R/x86_64-pc-linux-gnu-library/4.4" "1.0.2" NA "R6" "R6" "/cluster/medbow/home/<username>/R/x86_64-pc-linux-gnu-library/4.4" "2.5.1" NA "rlang" "rlang" "/cluster/medbow/home/<username>/R/x86_64-pc-linux-gnu-library/4.4" "1.1.4" NA "stringi" "stringi" "/cluster/medbow/home/<username>/R/x86_64-pc-linux-gnu-library/4.4" "1.8.4" NA "stringr" "stringr" "/cluster/medbow/home/<username>/R/x86_64-pc-linux-gnu-library/4.4" "1.5.1" NA "tibble" "tibble" "/cluster/medbow/home/<username>/R/x86_64-pc-linux-gnu-library/4.4" "3.2.1" NA "tidyr" "tidyr" "/cluster/medbow/home/<username>/R/x86_64-pc-linux-gnu-library/4.4" "1.3.1" NA "tidyselect" "tidyselect" "/cluster/medbow/home/<username>/R/x86_64-pc-linux-gnu-library/4.4" "1.2.1" NA "utf8" "utf8" "/cluster/medbow/home/<username>/R/x86_64-pc-linux-gnu-library/4.4" "1.2.4" NA "vctrs" "vctrs" "/cluster/medbow/home/<username>/R/x86_64-pc-linux-gnu-library/4.4" "0.6.5" NA "withr" "withr" "/cluster/medbow/home/<username>/R/x86_64-pc-linux-gnu-library/4.4" "3.0.0" NA "base" "base" "/apps/u/spack/gcc/14.2.0/r/4.4.0-w7xoohc/rlib/R/library" "4.4.0" "base" "compiler" "compiler" "/apps/u/spack/gcc/14.2.0/r/4.4.0-w7xoohc/rlib/R/library" "4.4.0" "base" "datasets" "datasets" "/apps/u/spack/gcc/14.2.0/r/4.4.0-w7xoohc/rlib/R/library" "4.4.0" "base" "graphics" "graphics" "/apps/u/spack/gcc/14.2.0/r/4.4.0-w7xoohc/rlib/R/library" "4.4.0" "base" "grDevices" "grDevices" "/apps/u/spack/gcc/14.2.0/r/4.4.0-w7xoohc/rlib/R/library" "4.4.0" "base" "grid" "grid" "/apps/u/spack/gcc/14.2.0/r/4.4.0-w7xoohc/rlib/R/library" "4.4.0" "base" "methods" "methods" "/apps/u/spack/gcc/14.2.0/r/4.4.0-w7xoohc/rlib/R/library" "4.4.0" "base" "parallel" "parallel" "/apps/u/spack/gcc/14.2.0/r/4.4.0-w7xoohc/rlib/R/library" "4.4.0" "base" "splines" "splines" "/apps/u/spack/gcc/14.2.0/r/4.4.0-w7xoohc/rlib/R/library" "4.4.0" "base" "stats" "stats" "/apps/u/spack/gcc/14.2.0/r/4.4.0-w7xoohc/rlib/R/library" "4.4.0" "base" "stats4" "stats4" "/apps/u/spack/gcc/14.2.0/r/4.4.0-w7xoohc/rlib/R/library" "4.4.0" "base" "tcltk" "tcltk" "/apps/u/spack/gcc/14.2.0/r/4.4.0-w7xoohc/rlib/R/library" "4.4.0" "base" "tools" "tools" "/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"

Remember: R Versions and Library Locations

Use Case: If projects/scripts are using say: module load r/4.3.x:

image-20240823-141510.png

The scripts in folder01/folder02 will both use/share the R packages under ~/R/x86_64-pc-linux-gnu-library/4.3

If you update an R package due to a need for a script in folder01, then this new R package will also be used by the scripts in folder02.

Is this intended? Does it course an issue for scripts in folder02? You need to be aware and manage.

Similarly: If projects/scripts are using: module load r/4.4.x:

image-20240823-141551.png

The scripts in folder03/folder04 will both use/share the R packages under ~/R/x86_64-pc-linux-gnu-library/4.4

If you update an R package due to modifying a script in folder03, then this new R package will also be used by the scripts in folder04.

They will not be using any r/4.3.x related packages - that’s a different library location.

In general: Using r/x.y.z packages will be found under ~/R/x86_64-pc-linux-gnu-library/X.Y


 

 

Related content