R¶

The R programming language is an integrated suite of software facilities for data manipulation, calculation, and graphical display. It is commonly used for data analysis, statistics, and visualization.
It provides a wide variety of statistical tools, such as linear and nonlinear modeling, classical statistical tests, time-series analysis, classification, clustering, and publication-quality plots.
The R module has been retired
NERSC no longer provides an R module. Please install your own R environment with conda as described below.
Installing R at NERSC¶
We recommend using Conda to install R and the packages you need.
Installing Your Own R Environment¶
We recommend installing your environment to your project's directory on /global/common/software (see Moving your conda setup to /global/common/software), which makes it easy to share with your project members. Use umask 002 first if you want other members of your project to be able to update the environment. You can also install to your home directory for a personal environment.
module load conda
conda create --yes --channel conda-forge \
--prefix /global/common/software/<project>/r-env \
r-base r-essentials r-irkernel <additional R libs>
conda activate /global/common/software/<project>/r-env
Tip
The retired NERSC R module was built with R 4.3.3 and included r-essentials, r-irkernel, r-raster, r-multicool, r-devtools, r-rserve, and r-dichromat. Add r-base=4.3.3 and these packages to the command above if you need a similar environment.
See our conda documentation for further information on managing Conda environments.
Adding Source Packages¶
Not all R packages are available on the conda-forge repositories, in which case you will likely need to install packages from source.
We recommend using the R install.packages function on top of your Conda installation, putting your source-installed packages in a dedicated library for each environment to avoid having various projects interfere with each other. Setting R_LIBS_USER as an environment variable of the conda environment makes R use that library whenever the environment is active:
conda activate /global/common/software/<project>/r-env
# Creating a dedicated folder for your R-installed dependencies
mkdir -p ~/.R/srclib/r-env
# Making R use that folder whenever this environment is active
conda env config vars set R_LIBS_USER=$HOME/.R/srclib/r-env
# Re-activating the environment for the variable to take effect
conda deactivate
conda activate /global/common/software/<project>/r-env
# Starting R and installing dependencies into the dedicated folder
R
> install.packages(<source pkg>)
Each environment can have its own R_LIBS_USER. For example, a second environment could use ~/.R/srclib/even-more-r.
Running R at NERSC¶
Once your conda environment is activated, you can call the R command and start R. However, resource-intensive work should not be run on login nodes. Use compute nodes for such work. See our Interactive Jobs page, our page on running jobs, and our example job scripts for how to run on compute nodes. Remember to module load conda and activate your R environment in your job.
Using Your Own R Environment in Jupyter¶
You can create a Jupyter kernel based on your own (conda-based) R environment. To do so, you will need the r-irkernel package (included in the command above) and a kernelspec file.
The following commands will set up r-irkernel within your R environment (run conda install -c conda-forge r-irkernel first if it is not already installed):
# Setting up IRkernel in R
conda activate /global/common/software/<project>/r-env
R
> ename <- basename(Sys.getenv('CONDA_DEFAULT_ENV'))
> dname <- trimws(paste("R", getRversion(), Sys.getenv("CONDA_PROMPT_MODIFIER")))
> IRkernel::installspec(name=ename, displayname=dname)
> quit()
The kernel is installed in your own Jupyter kernels directory, so each user who wants it needs to run installspec themselves.
Fixing the Jupyter Display¶
Some R functions do not display outputs properly in Jupyter (for example, system('ls') returns no output!). This is typically due to those functions sending outputs to stdout, instead of returning strings (as system('ls', intern=TRUE) would do).
Most of this behavior can be fixed by adding the following snippet to your .Rprofile and using system.jup from notebooks:
system.jup <- function(command) {
cat(base::system(command, intern=TRUE), sep='\n')
}
Parallel R¶
There are many ways to run R code in parallel, especially if you can use a coarse-grained parallelism pattern, in which chunks of computation can be computed independently from each other.
The following example illustrates using the parallel package, which creates workers as lightweight processes via forking, to optimize codes that use lapply, sapply, apply, and related functions:
library("parallel")
f = function(x) {
sum = 0
for (i in seq(1, x)) sum = sum + i
return(sum)
}
n = 1000
nCores <- as.integer(Sys.getenv("SLURM_CPUS_PER_TASK", detectCores()))
result = mclapply(X=1:n, FUN=f, mc.cores=nCores)
Inside a job, this uses the CPUs allocated by Slurm rather than the number of hardware threads on the node.
To go further, we recommend taking a look at the HPC with R Workshop slides. You might also find our Python documentation helpful, as it goes into detail on how one would use an interpreted language efficiently on an HPC system.
Getting Help with R¶
If you have R questions or problems, please contact NERSC's online help desk. We also encourage you to take a look at the following links:
- The Official R Project Documentation
- Efficient R Programming, a principled R guide
- Advanced R, a guide to deepen your understanding of R and get familiar with it if you come from another programming language
- The R Inferno, a guide to R's trouble spots, oddities, traps, and glitches
- HPC with R, a workshop delving deeper into using R on a high-performance system