Install & Compatibility
Where this runs
tested against v? · pip install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.910 runs
build_error
glibcpy 3.10–3.910 runs
build_error
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
initr
✓ from rpy2.rinterface import initr
✗ from rpy2.rinterface import R
The 'R' class for interpreter access was removed in v3.6.0. Use initr() and module-level attributes like baseenv instead.
baseenv
✓ from rpy2.rinterface import baseenv
evalr
✓ from rpy2.rinterface import evalr
evalr is a utility function to evaluate R code, though direct eval() on parsed Sexp objects is also common.
This quickstart demonstrates how to initialize the R interpreter, evaluate R code, and call R functions using `rpy2.rinterface`. It highlights the crucial step of calling `initr()` and interacting with R's base environment. Users must have R installed on their system and ensure it's discoverable (e.g., via `R_HOME` environment variable or system PATH).
import os
import rpy2.rinterface as rinterface
# IMPORTANT: rpy2-rinterface requires a functional R installation on your system.
# If R is not in your system's PATH, you might need to set R_HOME:
# os.environ['R_HOME'] = '/path/to/R'
# os.environ['PATH'] += os.pathsep + os.path.join(os.environ['R_HOME'], 'bin')
try:
# Initialize the R interpreter
# This must be called before interacting with R.
rinterface.initr()
print("R interpreter initialized successfully.")
# Access R's base environment
base = rinterface.baseenv
# Evaluate R code directly as a string
r_code = 'x <- 1:10; mean(x)'
result = rinterface.parse(r_code).eval(base)
print(f"R evaluation (mean of 1:10): {result[0]} (R type: {result.rid})")
# Call an R function with Python data
r_sum_func = base['sum']
python_list = [10, 20, 30]
r_vector = rinterface.IntSexpVector(python_list)
sum_result = r_sum_func(r_vector)
print(f"Sum of {python_list} in R: {sum_result[0]}")
except rinterface.RRuntimeError as e:
print(f"R Runtime Error: {e}\nEnsure R is installed and R_HOME is correctly set if needed.")
except Exception as e:
print(f"An unexpected error occurred: {e}")
Debug
Known issues
breakingThe `rpy2.rinterface.R` class, which was previously used to access the R interpreter, was removed in version 3.6.0. Directly instantiating `R()` is no longer possible.fixInstead of `from rpy2.rinterface import R; r_instance = R()`, use `import rpy2.rinterface as rinterface; rinterface.initr(); base = rinterface.baseenv; globalenv = rinterface.globalenv` to initialize R and access global environments.
affects: >=3.6.0
gotcharpy2-rinterface requires a pre-existing R installation on the system. If R is not installed or not discoverable in the system's PATH, Python will fail to load the R shared library.fixInstall R (https://cran.r-project.org/). If `rpy2` still fails to find it, set the `R_HOME` environment variable to the root directory of your R installation (e.g., `C:\Program Files\R\R-4.x.x` on Windows, `/usr/local/lib/R` on Linux/macOS) and ensure the R bin directory is in your system's PATH.
affects: All
gotchaOn Windows, loading the R shared library can be finicky. The `RPY2_CFFI_MODE` environment variable can switch between `API` and `EXT` modes, which might resolve issues.fixIf encountering loading errors on Windows, try setting `os.environ['RPY2_CFFI_MODE'] = 'API'` before importing `rpy2.rinterface`. The default is `EXT`.
affects: >=3.6.5 (API mode working on Windows)
Upgrade
Version history
3.6.6latest on PyPI · released Mar 27, 2026
Audit
Dependencies
R (programming language)requiredRequires a working R installation (base distribution) on the system. R_HOME environment variable or system PATH often needs to be configured.