
Compile an HPC-Ready Apptainer / Singularity Definition File
Source:R/compile_apptainer.R
compile_apptainer.RdGenerates an Apptainer/Singularity .def recipe from an rcapsule_scan
(or project directory path) suitable for university and national HPC
clusters. Includes pinned Bootstrap: docker / From: headers, %labels,
%files, %environment, strict set -euo pipefail %post provisioning
for OS system libraries, scientific toolchains, and R packages, standard
HPC bind mount points, %runscript, and a %test verification suite.
Usage
compile_apptainer(
scan,
output = NULL,
base_image = NULL,
r_version = NULL,
base_digest = NULL,
cran_repo = "https://packagemanager.posit.co/cran/2026-01-15",
quarto_version = "1.5.57",
cmdstan_version = "2.35.0",
workdir = "/capsule",
entrypoint = NULL,
allow_unpinned = FALSE
)Arguments
- scan
An
rcapsule_scanobject fromscan_project()or a path to a project directory.- output
Optional file path to write the
.defrecipe (e.g.,"Apptainer.def"). IfNULL, returns the compiled spec in memory.- base_image
Optional pinned Docker base image (defaults to
paste0("rocker/r-ver:", r_version)).- r_version
Optional R version string overriding
scan$r_version.- base_digest
Optional SHA-256 image digest appended to
base_image.- cran_repo
Pinned CRAN or Posit Public Package Manager repository URL.
- quarto_version
Pinned Quarto CLI version when Quarto is detected.
- cmdstan_version
Pinned CmdStan version when CmdStan is detected.
- workdir
Project directory inside the container image. Defaults to
"/capsule".- entrypoint
Optional override for the
%runscriptentrypoint.- allow_unpinned
Logical; if
FALSE(default), rejects:latestor untagged base images.
Value
An S3 object of class c("rcapsule_apptainer", "rcapsule_spec")
with fields content, lines, base_image, r_version, sections,
project_type, path, def_path, sif_path, binds, def_sha256,
content_digest, and scan.
Examples
tmp <- tempfile("rcapsule_apptainer_")
dir.create(tmp)
writeLines(c("library(stats)", "cat(mean(1:10), '\\n')"), file.path(tmp, "run.R"))
def <- compile_apptainer(tmp)
cat(def$content)
#> # Generated by rcapsule 0.6.0
#> # Project: rcapsule_apptainer_1a6362df5822 (analysis) | R 4.6.2
#> Bootstrap: docker
#> From: rocker/r-ver:4.6.2
#>
#> %labels
#> Project rcapsule_apptainer_1a6362df5822
#> ProjectType analysis
#> RVersion 4.6.2
#> GitRevision uncommitted
#> CreatedBy rcapsule-0.6.0
#>
#> %files
#> . /capsule
#>
#> %environment
#> export LC_ALL=C.UTF-8
#> export LANG=C.UTF-8
#> export TZ=UTC
#> export CAPSULE_DIR=/capsule
#>
#> %post
#> set -euo pipefail
#> export DEBIAN_FRONTEND=noninteractive
#> export TZ=UTC
#> export R_REPOS=https://packagemanager.posit.co/cran/2026-01-15
#>
#> # OS system dependencies
#> apt-get update \
#> && apt-get install -y --no-install-recommends \
#> ca-certificates \
#> && rm -rf /var/lib/apt/lists/*
#>
#> cd /capsule
#>
#> # HPC compatibility: create standard bind mount points & non-root read permissions
#> mkdir -p /scratch /work /data
#> chmod -R a+rX /capsule
#>
#> %runscript
#> cd /capsule
#> exec Rscript "run.R" "$@"
#>
#> %test
#> set -euo pipefail
#> R --version
unlink(tmp, recursive = TRUE)