R package installation guide

A guide for installing certain R packages that require extra steps using the software modules available on CARC HPC clusters.

The following instructions are based on the R 4.4.0 module:

module purge
module load gcc/11.3.0
module load openblas/0.3.20
module load r/4.4.0

They should work for any R version based on gcc/11.3.0 and openblas/0.3.20. Older R versions or older versions of R packages may require different steps.

Installing R packages from source

On Linux, R packages are installed from source by default. For some R packages, certain modules need to be loaded to install the package (build dependencies) and the same modules may also need to be loaded in order to load and use the R package (run dependencies). Packages that require specific steps to install on CARC HPC clusters are detailed below.

If using the RStudio Server app via CARC OnDemand, use the terminal tab within RStudio to install the packages listed below on the command line. Load the required modules in the shell, then enter R to open an R session and install packages. In some cases you may need to load additional modules in future RStudio Server sessions via the “Additional software modules to load” option on the app form in order to use the packages.

jpeg

In the shell:

module load libjpeg
export JPEG_CFLAGS=-I$LIBJPEG_ROOT/include
export JPEG_LIBS=-L$LIBJPEG_ROOT/lib

In R:

install.packages("jpeg")

When using the package, make sure to module load libjpeg first.

units

In the shell:

module load openmpi/4.1.4 sqlite geos gdal proj udunits

In R:

install.packages("units", configure.args = "--with-udunits2-lib=${UDUNITS_ROOT}/lib --with-udunits2-include=${UDUNITS_ROOT}/include")

udunits2

In the shell:

module load openmpi/4.1.4 sqlite geos gdal proj udunits

In R:

install.packages("udunits2", configure.args = "--with-udunits2-lib=${UDUNITS_ROOT}/lib --with-udunits2-include=${UDUNITS_ROOT}/include")

sf

In the shell:

module load openmpi/4.1.4 sqlite geos gdal proj udunits

In R:

install.packages("sf", configure.args = "--with-sqlite3-lib=${SQLITE_ROOT}/lib")

When using the package, make sure to module load openmpi/4.1.4 sqlite geos gdal proj udunits first.

terra

In the shell:

module load openmpi/4.1.4 sqlite geos gdal proj udunits

In R:

install.packages("terra", configure.args = "--with-sqlite3-lib=${SQLITE_ROOT}/lib")

When using the package, make sure to module load openmpi/4.1.4 sqlite geos gdal proj udunits first.

rJava

In the shell:

module load zlib bzip2 libiconv icu4c openjdk

In R:

install.packages("rJava", configure.args="CPPFLAGS='-I${ZLIB_ROOT}/include -I${BZIP2_ROOT}/include -I${LIBICONV_ROOT}/include -I${ICU4C_ROOT}/include' LDFLAGS='-L${ZLIB_ROOT}/lib -L${OPENJDK_ROOT}/lib/server -L${BZIP2_ROOT}/lib -L${LIBICONV_ROOT}/lib -L${ICU4C_ROOT}/lib'")

gurobi

In the shell:

module load gurobi/10.0.0

In R:

install.packages("slam")
install.packages("/spack/generic/gurobi/10.0.0/R/gurobi_10.0-0_R_4.2.0.tar.gz", repos = NULL)

When using the package, make sure to module load gurobi/10.0.0 first.

openblasctl

In R:

install.packages("openblasctl", repos = "https://hpcran.org", configure.args = "--with-openblas-libpath=${OPENBLAS_ROOT}/lib")

pbdMPI

Install on a login node.

In the shell:

module load pkgconf openmpi/4.1.4

In R:

install.packages("pbdMPI")

When using the package, make sure to module load openmpi/4.1.4 first.

Rmpi

Install on a login node.

In the shell:

module load pkgconf openmpi/4.1.4

In R:

install.packages("Rmpi")

When using the package, make sure to module load openmpi/4.1.4 first.

Adding compiler flags

When installing R packages from source with compiled programs, you can add custom compiler flags in ~/.R/Makevars. Adding optimization flags may provide a boost in performance for some packages. The following setup is used in the site-wide Makevars.site for each R module:

STDFLAGS = -O2 -pipe -march=x86-64-v3 -mtune=haswell
CFLAGS = ${STDFLAGS}
C17FLAGS = ${STDFLAGS}
C23FLAGS = ${STDFLAGS}
C90FLAGS = ${STDFLAGS}
C99FLAGS = ${STDFLAGS}
CXXFLAGS = ${STDFLAGS}
CXX11FLAGS = ${STDFLAGS}
CXX14FLAGS = ${STDFLAGS}
CXX17FLAGS = ${STDFLAGS}
CXX20FLAGS = ${STDFLAGS}
CXX20FLAGS = ${STDFLAGS}
CXX23FLAGS = ${STDFLAGS}
FCFLAGS = ${STDFLAGS}
FFLAGS = ${STDFLAGS}

Copy the site-wide Makevars to ~/.R/Makevars and then customize as needed:

mkdir $HOME/.R
cp $R_ROOT/rlib/R/etc/Makevars.site $HOME/.R/Makevars

You can also target specific CPU microarchitectures if you want. For example, to optimize compiled programs for AMD Zen 3 CPUs, you could change STDFLAGS to:

STDFLAGS = -O2 -pipe -march=znver3 -mtune=znver3

But note that this means these packages will only work on AMD Zen 3 CPUs (such as the epyc-7513 nodes on the epyc-64 partition on Discovery).