Loading R via module load R/version configures the read-only location $R_HOME/library where system wide packages are installed. R will fallback to installing in the user's home directory if it detects that the default library location is not writable. Custom install locations are also supported by environment variables and options to package and library functions.
Important Note: The R versions below R/4.4.0 have serious vulnerabilities. Please use the newer version module.
The default user install location for R is ~/R/x86_64-pc-linux-gnu-library/version/ and R creates the directory if it does not exist:
> install.packages("yaml")
Warning in install.packages("yaml") :
'lib = "/usr/local/easybuild_allnodes/software/R…" is not writable
Would you like to use a personal library instead? (yes/No/cancel) yes
Would you like to create a personal library
‘~/R/x86_64-pc-linux-gnu-library/4.1’
to install packages into? (yes/No/cancel) yes
An alternative is to set the environment variable R_LIBS_USER to a writable location. E.g.,
export R_LIBS_USER=~/Rpackages
The path will be searched (along with the system location) for packages to load, and will be used for installing user packages.
A different location can be specified with the lib parameter. R will not create the directory if it does not exist and R will not create version specific libraries under the directory.
install.packages("yaml", lib="~/Rpackages")
We must pass the location when loading libraries installed there:
library("yaml", lib.loc="~/Rpackages")
salloc -c 8 --mem=24g srun --pty /bin/bash
cd $TMPDIR
module load singularity
singularity build --sandbox ./R docker://r-base:latest
singularity shell --writable ./R
Singularity> mkdir -p /opt/mylib
Singularity> export R_LIBS_USER=/opt/mylib
Singularity> R
> install.packages("yaml")
...
> quit()
Singularity> chmod -R o+rX /opt/mylib
Singularity> exit
singularity build my_R.sif ./R
cp my_R.sif ~/
rm -rf ./R my_R.sif
Invoke Interactively
salloc -c 8 --mem=24g srun --pty /bin/bash
module load singularity
singularity run --env R_LIBS_USER=/opt/mylib my_R.sif R
> library(yaml)
Invoke in Batch Job
R Script my.R
library("yaml")
f = c(1, 2, 3, 4)
yaml::as.yaml(f)
Job File my_job.sh
#!/bin/bash
module load singularity
singularity run --env R_LIBS_USER=/opt/mylib my_R.sif Rscript my.R
Job Submit Command
sbatch my_job.sh
Options can be specified using the configure.args parameter:
> install.packages("https://cran.r-project.org/src/contrib/Rmpi_0.7-3.3.tar.gz", configure.args=c("--with-mpi=/usr/local/easybuild_allnodes/software/OpenMPI/4.1.6-GCC-13.2.0/"))
Note: download the source archive and run ./configure --help to see what options are available if the package does not document them
Missing openssl/ssl.h
If you see this error while installing from a compute node, install the failing package from a login node instead. Those have the OpenSSL development files installed.
gtar command not found
If you see this error while installing packages from inside Rstudio, you need to install them from a native R sessions first. Then you will be able to import them into your Rstudio session.