Overview
scimagR provides R wrappers for the Spinal Cord Toolbox (SCT)
command-line tools. These wrappers handle: - Path resolution (respects
SCT_DIR environment variable) - Input validation -
Idempotent execution (skip if output exists) - Structured error
reporting - Optional QC report generation
Configuration
Set the SCT_DIR environment variable if SCT is not on
your PATH:
Sys.setenv(SCT_DIR = "/path/to/sct")
Or add it to your .Renviron:
SCT_DIR=/path/to/sct
Spinal Cord Segmentation
library(scimagR)
sc_seg <- sct_segment_sc(
input = "sub-01_T2w.nii.gz",
qc_dir = "qc/"
)
Lesion Segmentation (SCIsegV2)
lesion_seg <- sct_segment_lesion(
input = "sub-01_T2w.nii.gz",
qc_dir = "qc/"
)
Vertebral Labeling
vert_labels <- sct_label_vertebrae(
input = "sub-01_T2w.nii.gz",
segmentation = sc_seg
)
QC Reports
Generate HTML QC reports to visually verify segmentations:
sct_generate_qc(
input = "sub-01_T2w.nii.gz",
segmentation = sc_seg,
qc_dir = "qc/"
)
Idempotent Execution
All SCT wrappers are idempotent by default. If the output file
already exists, the function returns the existing path without
re-running:
# First call: runs segmentation
sct_segment_sc("sub-01_T2w.nii.gz")
# Second call: skips, returns existing path
sct_segment_sc("sub-01_T2w.nii.gz")
# Force re-run:
sct_segment_sc("sub-01_T2w.nii.gz", overwrite = TRUE)
Troubleshooting
Common issues:
- “Cannot find sct_deepseg on PATH”: Set
SCT_DIR or add SCT bin/ to PATH
- Segmentation fails silently: Check stderr output in
the returned result
- Python errors: Ensure the correct Python
environment is activated