R Environment Guide#

The rebuilt package keeps R tools optional. Python-only workflows should not import rpy2 or any R package. R-backed methods live under sctk.r.

Python Side#

Recommended: install Python and R together with conda/mamba:

mamba env create -f environment.yml
conda activate sctk

This installs rpy2, anndata2ri, R, and the main Bioconductor packages into one environment.

R Side#

If you are not using conda for R, install R and Bioconductor packages in the R environment seen by rpy2.

Fallback:

Rscript requirements-r.R

The Bioconductor package page for scDblFinder currently documents BiocManager::install("scDblFinder") for the release version.

Check That Python Sees R#

python -c "import rpy2.robjects as ro; print(ro.r('R.version.string')[0])"

Check R packages:

python -c "from sctk.r import require_r_package; require_r_package('scDblFinder'); print('ok')"

Blocked pseudobulk differential expression additionally requires edgeR and limma, both included in environment.yml and requirements-r.R.

Example: scDblFinder#

import sctk

# Writes columns to adata.obs:
# - scDblFinder_score
# - scDblFinder_class
sctk.r.pp.scdblfinder(
    adata,
    layer="counts",
    batch_key="sample",
    n_jobs=4,
)

# Or return a filtered copy:
adata_singlet = sctk.r.pp.scdblfinder(
    adata,
    layer="counts",
    batch_key="sample",
    drop_doublets=True,
)

Example: blocked limma-voom#

de, diagnostics = sctk.r.tl.run_voom(
    pb,
    design="~ 0 + condition",
    contrast="conditiontreated-conditioncontrol",
    block_key="patient",
)

block_key may contain a mixture of repeated and singleton blocks. Singleton samples are retained while duplicateCorrelation estimates correlation from the repeated blocks.

Design Rule#

Every future R wrapper should follow the same pattern:

  • R imports happen inside the function, never at package import time.

  • Missing R packages fail with a short message naming the missing package.

  • Data inputs use the same Python API names as non-R functions: adata, layer, batch_key, groupby, sample_key, condition_key, and key_added.