Skip to main content

Using R Spatiotemporal Packages

R spatiotemporal packages are often very hard to install on Sol or Phx due to their complicated dependency trees. This page provides some tested work-arounds.

Best Option: Mamba Environments

Create your own private environment

note

Check the bottom of this page for recommended package lists

Click here to expand...
module purge
module load mamba/latest

mamba create -n r-spatial \
-c conda-forge \
r-base=4.5 \
r-sf \
...

# Activate
mamba activate r-spatial

# Verify
R -e 'library(sf); sf_extSoftVersion()'

# Enter an R session
R

# Add more packages if needed
mamba install -c conda-forge r-exactextractr r-tmap r-mapview

# Pure R packages (no compiled code) can be installed from inside R
R -e 'install.packages("spdep", repos="https://cloud.r-project.org")'
caution

Always pin r-base to a major.minor version (e.g., r-base=4.4) to avoid solver conflicts. Install everything in a single command to avoid dependency conflicts. Only use the conda-forge channel, DO NOT add the r channel.

note

If the R package has a SystemRequirements field in its DESCRIPTION (check on CRAN), install it via mamba. Pure R packages are safe to install with install.packages().

Use a public environment interactively

module purge
module load mamba/latest
mamba info --envs

source activate r-spatial-4.4
R
> library(sf)

Use a mamba environment in sbatch jobs

#!/bin/bash
#SBATCH ...

module load mamba/latest
source activate r-spatial-4.5.1

# Run script
Rscript my_analysis.R

Use a mamba environment in Rstudio

Install reticulate R package and use it to call python from within Rstudio. Please check their document for more details: https://rstudio.github.io/reticulate/articles/calling_python.html


Alternative Options

Apptainer Container

Click here to expand...
# Run R inside the container
apptainer exec /packages/apps/simg/r/rocker-geospatial-r4.5.sif R

# Run a script
apptainer exec /packages/apps/simg/r/rocker-geospatial-r4.5.sif Rscript my_analysis.R

# Bind your Sol scratch to the data directory inside the apptainer
apptainer exec --bind /scratch/$USER:/data \
/packages/apps/simg/r/rocker-geospatial-r4.5.sif Rscript my_analysis.R

renv + Mamba

Click here to expand...
module purge
module load mamba/latest

# Set up mamba env with system libraries only
mamba create -n r-geo-base -c conda-forge r-base=4.4 gdal geos proj udunits2
mamba activate r-geo-base

Then inside R:

install.packages("renv")
renv::init()

# Install R packages — they'll compile against mamba's system libs
renv::install("sf")
renv::install("terra")
renv::install("stars")

# Lock the environment
renv::snapshot()

This gives you a renv.lock file that records exact R package versions, while mamba handles the tricky system libraries underneath.


Minimal Spatiotemporal

mamba create -n r-spatiotemporal -c conda-forge \
r-base=4.4 r-sf r-terra r-stars r-units

Full Geospatial Analysis

mamba create -n r-geospatial -c conda-forge \
r-base=4.4 \
r-sf r-terra r-stars r-raster \
r-gstat r-spacetime r-sftime \
r-tmap r-mapview r-leaflet \
r-exactextractr r-lwgeom \
r-ncdf4 r-tidyverse r-data.table

Climate / NetCDF Focused

mamba create -n r-climate -c conda-forge \
r-base=4.4 \
r-sf r-terra r-stars r-ncdf4 \
r-cdo r-climate4r r-units \
cdo nco