call-r-lib-in-python
Skill1 starsChanged 5 months agoMITView on GitHub
Use when Python must call an R package through rpy2, exchange pandas or NumPy data with R, fit R-native models, or diagnose R discovery, package-library, conversion, and result-extraction failures.
In this repository14 other files it gives its agents.
- AGENTS.mdAGENTS.md
- agent-skills-archive-orchestratorSkill
- code-commenterSkill
- code-reviewerSkill
- end-user-xp-improverSkill
- fp-developerSkill
- lesson-documenterSkill
- plan-designerSkill
- preferred-workflowSkill
- release-preparerSkill
- scientific-prose-editorSkill
- simple-code-writerSkill
- spec-driven-developerSkill
- wayback-link-fallbackSkill
The file
We split this skill into the rules it gives an agent
7 rules. About 0 are common, 5 are distinctive, and 2 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈5
Distinctive · estimate
This author's own choices. Worth a look.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 8
Use rpy2 when an R-only implementation is required in-process.
- Distinctive: line 14
Before debugging bridge code, verify the same environment that launches Python can run:
- Very specific: line 31
Check packages with requireNamespace(..., quietly = TRUE) and use namespace-qualified calls where practical.
- Distinctive: line 45
Read integration patterns for concrete rpy2 code.
- Distinctive: line 46
Read troubleshooting only when diagnosing a failure.
- Distinctive: line 54
Treat these as public boundary concerns:
- Distinctive: line 63
Stop and report when:
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Call R Libraries from Python
Use rpy2 when an R-only implementation is required in-process. Prefer a maintained
Python-native implementation when it satisfies the requirement; prefer an Rscript
subprocess when strict process or dependency isolation is necessary.
Preflight
Before debugging bridge code, verify the same environment that launches Python can run:
which R
R --version
python -c "import rpy2; print(rpy2.__version__)"Resolve R discovery, compatible R/rpy2 versions, and native build prerequisites before
investigating packages or conversion. Do not set R_HOME unless the installed rpy2/R
configuration requires it.
Integration workflow
- Choose and document a reproducible R dependency strategy. If packages are installed at
runtime, use a writable project-local library and configure
.libPaths()before package lookup. Do not silently mutate a global R installation. - Check packages with
requireNamespace(..., quietly = TRUE)and use namespace-qualified calls where practical. Installation is an explicit environment/setup action, not an incidental model-fitting side effect. - Convert data explicitly at the Python/R boundary with a scoped rpy2 converter. Keep the original R object when R semantics such as factors, formulas, attributes, or classes matter.
- Fit models with R-native inputs. Represent domain responses correctly—for example, grouped binomial models use successes and failures rather than an unexplained raw proportion.
- Extract stable, named results at the R boundary. Coerce optional
NULLvalues and complex S3/S4 structures in R before converting them to Python. - Return typed Python-facing values and retain version, warning, convergence, and failure evidence needed to interpret the result.
Read integration patterns for concrete rpy2 code. Read troubleshooting only when diagnosing a failure.
Design boundaries
Separate runtime discovery, dependency setup, conversion, and analysis. Keep R evaluation small and parameterized; do not interpolate untrusted values into R source strings. Prefer rpy2 objects, function calls, and explicit assignment over constructing executable code.
Treat these as public boundary concerns:
- Missing R, packages, compilers, or system libraries.
- Data-frame column types, missing values, factors, dates, and index semantics.
- R warnings, convergence messages,
NULL, S3/S4 objects, and scalar/vector shape. - Reproducible package versions and repositories.
Stop conditions
Stop and report when:
- R and rpy2 compatibility cannot be established from the active environment.
- Package installation needs system changes, credentials, or network access not authorized by the user.
- Conversion would discard domain-significant R attributes or classes.
- A model result cannot be extracted without relying on an undocumented package structure.
Completion checklist
- R discovery and relevant versions were recorded.
- Dependency setup is explicit and reproducible.
- Conversions are scoped and tested in both directions where required.
- R-native semantics are preserved during fitting and extraction.
- Warnings, optional values, and failures have stable Python representations.
- Tests cover representative data, missing/empty input, and at least one failure boundary.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

