Personal R Packages

We encourage users to maintain their own R packages. This ensures that the user has no discontinuity in their workflow waiting for packages to be installed and packages work with the user’s chosen, loaded R module. R packages are built specifically to a version of R and may not work properly if a different version of R is loaded. O2’s LMOD “module” feature allows users to quickly and easily switch between R versions.


Table of Contents

General Commands

Command

Meaning

Command

Meaning

module spider R

shows the version of R installed on O2

module spider R/<version>

shows if other module(s) needs to be loaded to use R (e.g., gcc/9.2.0)

module load gcc/9.2.0 R/4.1.2

loads an individual module (i.e., R/4.1.2)

module unload R/4.1.2

unloads an individual module (i.e., R/4.1.2)

which R

shows the current version of R loaded

> installed.packages()

From R, show a list of all installed R packages

Setting up a Personal R Library

This sets up an environment to store R packages locally. From R, all packages will be installed and saved to this R Personal Library by default. It is recommended to use a separate R library for each version of R selected (e.g., 3.6.1, 4.1.1, etc.). Multiple members of a group can share an R library saved in a group-accessible location by following the echo ... and export ... commands.

In O2 bash (hardcoded):

This approach is ideal for researchers using mainly a single R version and sharing the R Personal Library with other lab members.

mkdir -p ~/R-<version_selected>/library echo 'R_LIBS_USER="~/R-<version_selected>/library"' > $HOME/.Renviron export R_LIBS_USER="~/R-<version_selected>"

 

In O2 bash (non-hardcoded alternative):

This alternative approach is ideal for researchers working concurrently with more than one R version or switching often among R versions. In this approach, the R Personal Library is automatically set based on the R module loaded.

echo 'R_LIBS_USER="~/R/%p-library/%V"' > $HOME/.Renviron

Note: %V expands to Major.Minor.PatchLevel, while %p expands to the platform for which R was built. We recommend using %V to account for the PatchLevel and avoid issues with different gcc versions.

Starting R Interactively

R must be run in a compute node rather than a login node. To start an interactive session in a compute node, you need to specify the resources (i.e., walltime, cpus, and memory) via the $ srun command and specify interactive as the partition. For example:

# 1. Start an interactive job (resources: 1 hour, 5GB of memory, and 1 cpu) mfk8@login01:~$ srun --pty -p interactive -t 0-1:00 --mem 5G -c 1 bash # 2. Load R/4.1.2 mfk8@compute-a-16-163:~$ module load gcc/9.2.0 R/4.1.2 # 3. Start R mfk8@compute-a-16-163:~$ R > #notice how the prompt changes.

R does not implicitly use multiple core unless the code is parallelized. For researchers using the R package doParallel, please check Over Efficient Jobs | R package doParallel

Installing Packages Using Bioconductor

The Bioconductor repository host a wide range of bio-related packages. To install Bioconductor, please check the installation page on the Bioconductor website in case the method described below changes in the future.

Installing Packages Using CRAN

The command install.packages() searches CRAN by default and ask you to select a mirror, but a repository can be specified using this command:

User example:

A list of CRAN available packages is available at http://cran.r-project.org/web/packages/

Installing Packages from GitHub using Devtools

The R package devtools allow researchers to install software from GitHub repositories. First, you need to install devtools from the CRAN repo. Second, you need to load the R package using the library command. Lastly, you use the install_github() command to install R packages from GitHub. For example:

Installing Packages from a Local File

To install a package manually, place the compressed R package (usually is compressed as a tarball or zip file) inside your R Personal Library (e.g., ~/R-<version_selected>/library) and run the following command:

You can also install an R package via R CMD INSTALL <path-to-tarball>. However, this assumes all dependencies are already installed in your R Personal Library. For example:

Installing Packages with Special Requirements

We’ve created a Confluence page with step-by-step instructions to install some commonly used R packages (e.g., SF, monocle3, etc.) requiring a few [non-common] extra steps - Installing Other R packages on O2. Often, the extra steps involve installing a system library. To simplify the process, we have installed those system libraries as modules on O2.

Loading R packages

Once the package is installed, the library(<pkg_name>) can be used to load a package; for example: