R
R is an open source statistics package. Note, for specific instructions for using RStudio, see our RStudio documentation. There are some critical differences between how to use R and RStudio, including the processes to install packages.External Links:
CRAN R website
An Introduction to R
Loading R
module load RThere are various versions of R on Hazel. To see the available versions, use
module avail R.
To load a specific version of R, use:
module load R/4.5.0Note: Do not
module load R when using a Conda-based R environment. See the Conda section for details.
Running R
Do not use R on a login node for anything other than installing new R packages or other non-computational tasks. Before running an R job, verify whether the R functions used are serial or parallel.
Serial R
Here is an example batch script for a serial job. For more information on submitting batch scripts, see the documentation on running jobs.
To run R in batch mode using an R script called my_program.R, create a text file called submit.sh containing:
#!/bin/bash #SBATCH --time=00:20:00 # Program will terminate after 20 minutes #SBATCH --ntasks=1 #SBATCH --mem=20G # 20 GB of RAM reserved (Reserve memory if the job is memory intensive) #SBATCH --output=out.%j # stdout goes to a file called "out." #SBATCH --error=err.%j # stderr goes to a file called "err. " module load R Rscript my_program.R
The job can be submitted as
sbatch submit.sh
The script submit.sh requests one core and a memory of 20 GB RAM for 20 minutes. Check the documentation on Slurm options to customize the batch script, and see the documentation on requesting memory resources if the job is expected to be memory intensive.
For an example on submitting multiple R jobs, see the following sample R script for multiple job submissions. The script defines various years and models, and then it uses sbatch and Rscript to submit a separate job for each scenario.
Parallel R - shared memory
Using R in shared memory means that the program will use multiple threads on one node; there is no communication across nodes. R programs that do not use MPI have shared memory parallelism only. The following example requests one task with 8 CPUs and sets the number of OpenMP threads to match the Slurm allocation.
#SBATCH --ntasks=1 #SBATCH --cpus-per-task=8 # 8 threads will be working on the task at the same time #SBATCH --time=30 # Program will terminate after 30 minutes #SBATCH --mem=20G # 20 GB of RAM reserved (Reserve memory if the job is memory intensive) #SBATCH --output=out.%j # stdout goes to a file called "out." #SBATCH --error=err.%j # stderr goes to a file called "err. " module load R export OMP_NUM_THREADS=$SLURM_CPUS_PER_TASK Rscript my_program.R
Many R libraries and functions that are parallel automatically spawn as many threads as exist on a node. A user should either modify the R code to specifically limit the number of cores used, or request the appropriate number of CPUs for the job.
Parallel R - distributed memory
The pbdMPI package provides MPI bindings for R and is installed within a Conda environment, built against the system MPI. Follow the instructions to run a distributed memory R job on the cluster.
If Conda has not yet been set up, follow the instructions for initializing Conda.
Create a Conda environment and install pbdMPI.
conda create --prefix /usr/local/usrapps/$GROUP/$USER/env_pbdmpi r-base
conda activate /usr/local/usrapps/$GROUP/$USER/env_pbdmpi
module load openmpi-gcc/openmpi4.1.6-gcc13.2.0
R
> install.packages("pbdMPI", repos="https://cloud.r-project.org")
For example, to run the sample hello.R script on 8 tasks across 2 nodes using pbdMPI, create a slurm script called submit.sh containing:
#!/bin/bash #SBATCH --output=out.%j #SBATCH --error=err.%j #SBATCH --time=00:10:00 #SBATCH --ntasks=8 #SBATCH --nodes=2 source ~/.bashrc module load openmpi-gcc/openmpi4.1.6-gcc13.2.0 conda activate /usr/local/usrapps/$GROUP/$USER/env_pbdmpi mpirun -n 8 Rscript hello.R conda deactivateThe job can be submitted as
sbatch submit.sh
Interactive R
An interactive session may be used for short debugging or in order to prepare a production batch script. Interactive sessions must be kept to a minimum and only used when necessary. Nodes left idle or underutilized by long running interactive sessions may be terminated. To use R interactively for test and debug, use an interactive session on a compute node. To confirm that the session is not on a login node, type hostname.
1. Interactive R in the R console
The following requests an interactive session with 1 core for 10 minutes. The --exclusive means a request for the entire node (exclusive). Memory intensive R jobs (e.g. manipulating GeoTIFFs or rasters) should request the entire node regardless of cores needed. After the interactive session begins, open R.
salloc --ntasks=1 --exclusive --time=00:10:00 module load R RTo exit R, type
quit(). To exit the interactive session on the compute node, type exit.
2. Interactive R with a submit script
Another way to run R interactively is to create a Slurm script similar to a normal batch script, but without the #SBATCH lines. With interactive R, you can see your outputs/errors in the terminal. Load your R code and submit script to your working directory and run the script interactively by using a similar salloc command:
salloc --ntasks=1 --cpus-per-task=8 --mem=20G --time=00:30:00 ./submit.sh
Installing R Packages
R packages should be installed in /usr/local/usrapps/$GROUP/$USER/libs/R. Home directories are too small for most R package collections. If your group does not yet have a /usr/local/usrapps/$GROUP directory, request one here.
All package installations must be performed on a login node, as compute nodes cannot connect to the internet.
Setting up the R library path
Create the directory for your R packages:
mkdir -p /usr/local/usrapps/$GROUP/$USER/libs/R
Create a file called ~/.Renviron containing:
R_LIBS=/usr/local/usrapps/$GROUP/$USER/libs/R
Replace $GROUP and $USER with your actual group name and user name.
Then load R, enter the R console, and verify the library path:
module load R R > .libPaths()
The first path listed should be your /usr/local/usrapps directory.
Setting the environment for user installed packages
When using R libraries, the run environment should be the same as the compile environment. For example, if a certain module was loaded or the export command was used when installing a package, that same module or environment variable must be used when running the package.
Installing with install.packages
After setting up the R library path, install.packages() works as usual. Packages will install to the first path listed in .libPaths(), which should be your /usr/local/usrapps directory.
Package installations must be performed on a login node, as compute nodes cannot connect to the internet. On the login node, load R, enter R, and then run install.packages("packagename"). In some cases, you may need to specify a repository such as install.packages("packagename", repos="https://cloud.r-project.org").
When using multiple versions of R or multiple versions of libraries and packages, a user may create multiple library directories and specify the library locations when installing and when loading the libraries. Libraries installed under one version or compilation of R may not work with another version. To install to a specific library location, do
install.packages("packagename", lib="/usr/local/usrapps/$GROUP/$USER/libs/R_v2"). To load a library from a specific location, do library("packagename", lib.loc="/usr/local/usrapps/$GROUP/$USER/libs/R_v2").
Installing with Conda (recommended for complex dependencies)
Conda is the preferred method of installation for R packages that need external dependencies and newer compilers. Some R packages have complex dependencies on external libraries with varying versions and options required. When using install.packages, a user may have to install those dependencies manually. In this case, using a Conda environment is preferable.
See the instructions for installing software with Conda for general details about the procedure, which includes this YAML file for installing a set of R packages. Note that the example rlibs.yml is just an example; the packages included are not necessary for every environment.
Create a YAML file that includes every library call contained in a set of R scripts. Most packages have the naming convention r-packagename. Do a search to confirm whether a Conda package exists for the R library, and if so, to check the name and preferred channel. If an R library does not exist as a Conda package, then install.packages may be used in the general fashion using R while the Conda environment containing the other Conda-installed packages is active.
When installing R libraries via Conda, the system R is not used; Conda installs a different version of R based on the compatibility of the included libraries. Therefore do not module load R when running from a Conda-based R environment. Instead, use conda activate as described in the Conda documentation.
When using R in a Conda environment, remove any ~/.Renviron file, as Conda manages its own R and library paths. Remove hard links in R scripts to libraries installed with the system R.
Bioconductor
For bioinformatics, Bioconductor is a great tool. Install the newest base R in a new Conda environment, install the BiocManager, and use install.packages within that environment. BiocManager can also be used with the system R after setting up the R library path. Please see the Bioconductor documentation for more details and the latest version.
Last modified: September 01 2026 21:22:40.