Using R Packages
Overview
Note: because software modules are not available on the login nodes, you will need to be on a compute node to load R and install packages.
R is a popular language for data analysis and visualization. Different versions are available as software modules and we provide the graphical interface RStudio for R through our Open OnDemand web interface.
Similar to other languages that use package managers to install libraries contributed by the user community, we recommend you create and manage your own local libraries in your account. This ensures a stable global environment for all users and that you have the most control over your packages' versions and dependencies.
We provide instructions below for how to create, use, and switch between libraries as well as some debugging techniques for when package installations fail. We also provide some script examples (click the button in the banner at the top of this page) for submitting R scripts as batch jobs.
RStudio is a popular method for running analyses (and for good reason!), but for longer-running jobs (say, many hours or days) or workflows that need more flexibility in their environment (e.g., need access to software installed as system modules such as gdal), we recommend batch submissions.
Creating a Custom R Library
R packages can be finicky. See Switching Between Custom Libraries and Common Problems below to help with frequent user issues.
Creating Your First Library
Make a local directory to store your packages:
$ mkdir -p ~/R/library
Tell R where the directory is by creating an environment file:
$ echo 'R_LIBS=~/R/library/' >> ~/.Renviron
That's it! Now you can install packages normally. For example, to install and load the package "ggplot2":
$ module load R $ R > install.packages("ggplot2") > library(ggplot2)
Switching Between Custom Libraries
If you're using different versions of R, we recommend you use different libraries. See Common Problems below for more information. When creating a library, consider including pertinent information in the name such as R version. For example:
If you start by using R version 4.0, following the instructions provided above:
$ mkdir -p ~/R/library_R_v4.0 $ echo 'R_LIBS=~/R/library_R_v4.0/' >> ~/.Renviron
If you later decide to switch to R version 4.1, instead of using your existing library, create a new one:
$ mkdir -p ~/R/library_R_v4.1
To use your new library, edit your .Renviron file:
$ nano ~/.Renviron # opens the R environment file for editing > R_LIBS=~/R/library_R_v4.1 # delete ~/R/library_R_v4.0 and enter the new directory > CONTROL + X # exits (remember to save at the prompt)
Now you can go about your business and install as you normally would.
Common Problems and How to Debug Them
Working on a cluster without root privileges can lead to complications. For general information on package installations, see the r-bloggers documentation. For information on common installation problems on our clusters, see the section below with with suggested solutions: