---
title: "SCT Interface"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{SCT Interface}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
resource_files:
- figures/logo.svg
- figures/logo-dark.svg
---
[](https://github.com/CTTIR/scimagR/actions/workflows/R-CMD-check.yaml)
[](https://cttir.github.io/scimagR/)
[](https://CRAN.R-project.org/package=scimagR)
[](https://app.codecov.io/gh/CTTIR/scimagR?branch=main)
[](https://cran.r-project.org/package=scimagR)
[](https://cran.r-project.org/package=scimagR)
[](https://opensource.org/licenses/MIT)
[](https://lifecycle.r-lib.org/articles/stages.html#experimental)
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
eval = FALSE
)
```
## 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:
```{r sct-dir}
Sys.setenv(SCT_DIR = "/path/to/sct")
```
Or add it to your `.Renviron`:
```
SCT_DIR=/path/to/sct
```
## Spinal Cord Segmentation
```{r segment-sc}
library(scimagR)
sc_seg <- sct_segment_sc(
input = "sub-01_T2w.nii.gz",
qc_dir = "qc/"
)
```
## Lesion Segmentation (SCIsegV2)
```{r segment-lesion}
lesion_seg <- sct_segment_lesion(
input = "sub-01_T2w.nii.gz",
qc_dir = "qc/"
)
```
## Vertebral Labeling
```{r label-vert}
vert_labels <- sct_label_vertebrae(
input = "sub-01_T2w.nii.gz",
segmentation = sc_seg
)
```
## Parameter Extraction
```{r analyze}
# Lesion parameters (volume, length, tissue bridges)
lesion_csv <- sct_analyze_lesion(
lesion_seg = lesion_seg,
sc_seg = sc_seg
)
# Morphometric parameters (CSA, diameters per level)
csa_csv <- sct_process_segmentation(
sc_seg = sc_seg,
vert_labels = vert_labels
)
```
## QC Reports
Generate HTML QC reports to visually verify segmentations:
```{r qc}
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:
```{r idempotent}
# 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