Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Table of Contents
stylenone

...

Use Case

Info

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

Info

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

Code Block
# Create R Library folder:
[salexan5@mblog2 ~]$ cd /project/arcc/software/
[salexan5@mblog2 software]$ mkdir -p r_library/r_workshop
[salexan5@mblog2 r_library]$ cd r_library/r_workshop/
[salexan5@mblog2 r_workshop]$ pwd
/project/arcc<project-name>/software/r_library/r_workshop

# Load and Start R
[salexan5@mblog2 ~]$ module purge
[]$ module load gcc/13.2.0 r/4.4.0
[salexan5@mblog2 ~]$ R

# Check current library paths:
> .libPaths()
[1] "/cluster/medbow/home/salexan5<username>/R/x86_64-pc-linux-gnu-library/4.4"
[2] "/apps/u/spack/gcc/13.2.0/r/4.4.0-pvzi4gp/rlib/R/library"

...

Current Available R Packages

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

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

Info

Update the library paths to point to my newly created library folder, and the base packages.

Code Block
> .libPaths(c('/project/arcc<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/13.2.0/r/4.4.0-pvzi4gp/rlib/R/library" "4.4.0" "base"
...
"utils" "utils" "/apps/u/spack/gcc/13.2.0/r/4.4.0-pvzi4gp/rlib/R/library" "4.4.0" "base"

...

Code Block
# Within R:
> install.packages("Matrix")
...
also installing the dependency ‘lattice’
...
* DONE (Matrix)

> write.table(installed.packages()[,c(1,2,3:4)])
"Package" "LibPath" "Version" "Priority"
"lattice" "lattice" "/cluster/medbow/project/arcc<project-name>/software/r_library/r_workshop" "0.22-6" "recommended"
"Matrix" "Matrix" "/cluster/medbow/project/arcc<project-name>/software/r_library/r_workshop" "1.7-0" "recommended"
"base" "base" "/apps/u/spack/gcc/13.2.0/r/4.4.0-pvzi4gp/rlib/R/library" "4.4.0" "base"
...
"utils" "utils" "/apps/u/spack/gcc/13.2.0/r/4.4.0-pvzi4gp/rlib/R/library" "4.4.0" "base"

# Lets check:
[salexan5@mblog2 r_library]$ ls /project/<project-name>/software/r_library/r_workshop/
lattice  Matrix

...

Test and Use

Error:

Code Block
[salexan5@mblog1 r_library]$ cat r_library_test_fail.R
library(Matrix)

[salexan5@mblog1 r_library]$ Rscript r_library_test_fail.R
Error in library(Matrix) : there is no package called ‘Matrix’
Execution halted
# The Matrix package is NOT available under our default library location.

...

Note

Update the .libPaths() every time you wish to use this R library.

Code Block
[salexan5@mblog1 r_library]$ cat r_library_test.R
.libPaths(c('/project/arcc<project-name>/software/r_library/r_workshop', '/apps/u/spack/gcc/12.2.0/r/4.4.0-7i7afpk/rlib/R/library'))
library(Matrix)

[salexan5@mblog1 r_library]$ Rscript r_library_test.R
[salexan5@mblog1 r_library]$ 

...

Warning: Remember

Note

Within this example, by default this is a SHARED project location.

Anyone who has access to this project and thus this shared library can update it.

If an individual performs an install.packages() it will effect everyone.

...