R package installation guide

A guide for installing R packages using the modules available on CARC HPC clusters.

The following instructions are based on the R 4.3.0 module:

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

Older R versions 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 modules to load” option on the app form in order to use the packages.

tidyverse

In the shell:

module load pkgconf libpng libjpeg-turbo libtiff zlib bzip2 libxml2 curl fontconfig freetype graphite2 pcre harfbuzz fribidi glib openssl icu4c

In R:

install.packages("tidyverse")

data.table

Create/modify ~/.R/Makevars to include:

CPPFLAGS = -I${ZLIB_ROOT}/include
LDFLAGS = -L${ZLIB_ROOT}/lib

In the shell:

module load pkgconf zlib

In R:

install.packages("data.table")

XML

In the shell:

module load pkgconf libxml2 xz libiconv

In R:

install.packages("XML")

xml2

In the shell:

module load libxml2

In R:

install.packages("xml2")

openssl

In the shell:

module load openssl

In R:

install.packages("openssl")

curl

In the shell:

module load curl

In R:

install.packages("curl")

RCurl

In the shell:

module load curl libxml2

In R:

install.packages("RCurl")

systemfonts

In the shell:

module load pkgconf libpng zlib libxml2 fontconfig freetype

In R:

install.packages("systemfonts")

textshaping

In the shell:

module load pkgconf libpng zlib libxml2 fontconfig freetype graphite2 pcre harfbuzz fribidi glib

In R:

install.packages("textshaping")

ragg

In the shell:

module load pkgconf libpng libjpeg-turbo libtiff zlib bzip2 libxml2 fontconfig freetype graphite2 pcre harfbuzz fribidi glib

In R:

install.packages("ragg")

stringi

In the shell:

module load icu4c

In R:

install.packages("stringi")

igraph

In the shell:

module load libxml2

In R:

install.packages("igraph")

httpuv

Create/modify ~/.R/Makevars to include:

CPPFLAGS = -I${ZLIB_ROOT}/include
LDFLAGS = -L${ZLIB_ROOT}/lib

In the shell:

module load zlib automake autoconf libtool m4

In R:

install.packages("httpuv")

haven

In the shell:

module load zlib

In R:

install.packages("haven")

git2r

In the shell:

module load git libgit2 openssl zlib

In R:

install.packages("git2r")

Cairo

In the shell:

module load pkgconf libpng libjpeg-turbo libtiff zlib bzip2 libxml2 icu4c cairo pcre fontconfig freetype graphite2 pcre harfbuzz fribidi glib libx11 libxau libxt libxext libxcb libxdmcp libxrender xproto xextproto kbproto renderproto libffi pixman

In R:

install.packages("Cairo", repo = "https://rforge.net")

png

Create/modify ~/.R/Makevars to include:

CPPFLAGS = -I${ZLIB_ROOT}/include
LDFLAGS = -L${ZLIB_ROOT}/lib

In the shell:

module load zlib libpng

In R:

install.packages("png")

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")

rsvg

In the shell:

module load pkgconf librsvg glib gobject-introspection util-linux shared-mime-info cairo gdk-pixbuf pcre pixman fontconfig freetype zlib libxml2 libffi libx11 libxrender libxext libxcb libxau libxdmcp xproto renderproto kbproto xextproto

In R:

install.packages("rsvg")

ncdf4

In the shell:

module load openmpi/4.1.4 netcdf-c

In R:

install.packages("ncdf4")

hdf5r

Create/modify ~/.R/Makevars to include:

CPPFLAGS = -I${ZLIB_ROOT}/include
LDFLAGS = -L${ZLIB_ROOT}/lib

In the shell:

module load openmpi/4.1.4 hdf5 zlib

In R:

install.packages("hdf5r")

bigmemory

Create/modify ~/.R/Makevars to include:

CPPFLAGS = -I${LIBUUID_ROOT}/include
LDFLAGS = -L${LIBUUID_ROOT}/lib

In the shell:

module load libuuid

In R:

install.packages("bigmemory")

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")

rgdal

In the shell:

module load openmpi/4.1.4 sqlite geos gdal proj udunits

In R:

install.packages("rgdal")

s2

In the shell:

module load openssl

In R:

install.packages("s2")

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")

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")

rgeos

In the shell:

module load geos

In R:

install.packages("rgeos")

nloptr

In the shell:

module load cmake

In R:

install.packages("nloptr")

gdtools

In the shell:

module load fontconfig freetype cairo gobject-introspection glib libxcb libxext libxau renderproto libxrender libffi xproto kbproto zlib pixman libx11 libxml2 libxdmcp xextproto pcre libpng

In R:

install.packages("gdtools")

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'")

RcppGSL

In the shell:

module load gsl

In R:

install.packages("RcppGSL")

RcppZiggurat

Create/modify ~/.R/Makevars to include:

CPPFLAGS = -I${GSL_ROOT}/include
LDFLAGS = -L${GSL_ROOT}/lib

In the shell:

module load gsl

In R:

install.packages("RcppZiggurat")

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)

energy

In the shell:

module load gsl

In R:

install.packages("energy")

qqconf

In the shell:

module load openmpi/4.1.4 fftw

In R:

install.packages("qqconf")

openblasctl

In R:

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

float

Create/modify ~/.R/Makevars to include:

CPPFLAGS = -I${OPENBLAS_ROOT}/include
LDFLAGS = -L${OPENBLAS_ROOT}/lib

In R:

install.packages("float")

pbdMPI

Install on a login node.

In the shell:

module load pkgconf openmpi/4.1.4

In R:

install.packages("pbdMPI")

Rmpi

Install on a login node.

In the shell:

module load pkgconf openmpi/4.1.4

In R:

install.packages("Rmpi")

Signac

Create/modify ~/.R/Makevars to include:

CPPFLAGS = -I${ZLIB_ROOT}/include
LDFLAGS = -L${ZLIB_ROOT}/lib

In the shell:

module load zlib

In R:

install.packages("Signac")

Bioconductor packages

Rhtslib

Create/modify ~/.R/Makevars to include:

CPPFLAGS = -I${ZLIB_ROOT}/include -I${BZIP2_ROOT}/include -I${XZ_ROOT}/include -I${CURL_ROOT}/include
LDFLAGS = -L${ZLIB_ROOT}/lib -L${BZIP2_ROOT}/lib -L${XZ_ROOT}/lib -L${CURL_ROOT}/lib

In the shell:

module load zlib bzip2 xz curl

In R:

BiocManager::install("Rhtslib")

XVector

Create/modify ~/.R/Makevars to include:

CPPFLAGS = -I${ZLIB_ROOT}/include
LDFLAGS = -L${ZLIB_ROOT}/lib

In the shell:

module load zlib

In R:

BiocManager::install("XVector")

Rsamtools

Create/modify ~/.R/Makevars to include:

CPPFLAGS = -I${ZLIB_ROOT}/include -I${XZ_ROOT}/include -I${CURL_ROOT}/include
LDFLAGS = -L${ZLIB_ROOT}/lib -L${XZ_ROOT}/lib -L${CURL_ROOT}/lib

In the shell:

module load zlib xz curl

In R:

BiocManager::install("Rsamtools")

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. Something like the following is a good starting point:

STDFLAGS = -g -O3 -pipe -Wall -march=x86-64-v3 -mtune=haswell
CFLAGS = ${STDFLAGS}
CXXFLAGS = ${STDFLAGS}
CXX11FLAGS = ${STDFLAGS}
CXX14FLAGS = ${STDFLAGS}
CXX17FLAGS = ${STDFLAGS}
CXX20FLAGS = ${STDFLAGS}
FCFLAGS = ${STDFLAGS}
FFLAGS = ${STDFLAGS}

This setup will work across most CPU models on CARC HPC clusters (except older models without AVX2 instructions, such as on the oneweek partition on Discovery).

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 = -g -O3 -pipe -Wall -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).