{"id":10204,"library":"rpy2-rinterface","title":"Rpy2 R Interface (rpy2-rinterface)","description":"rpy2-rinterface provides a low-level CFFI-based interface between Python and the R programming language, allowing Python code to directly interact with the R interpreter, R objects, and R functions at a granular level. It forms the foundational layer for higher-level Rpy2 packages. The current version is 3.6.6, with frequent patch releases addressing bug fixes and minor improvements, and new minor versions released periodically.","status":"active","version":"3.6.6","language":"en","source_language":"en","source_url":"https://github.com/rpy2/rpy2","tags":["R","data science","interoperability","ffi","cffi"],"install":[{"cmd":"pip install rpy2-rinterface","lang":"bash","label":"Install core interface"}],"dependencies":[{"reason":"Requires a working R installation (base distribution) on the system. R_HOME environment variable or system PATH often needs to be configured.","package":"R (programming language)","optional":false}],"imports":[{"note":"The 'R' class for interpreter access was removed in v3.6.0. Use initr() and module-level attributes like baseenv instead.","wrong":"from rpy2.rinterface import R","symbol":"initr","correct":"from rpy2.rinterface import initr"},{"symbol":"baseenv","correct":"from rpy2.rinterface import baseenv"},{"note":"evalr is a utility function to evaluate R code, though direct eval() on parsed Sexp objects is also common.","symbol":"evalr","correct":"from rpy2.rinterface import evalr"}],"quickstart":{"code":"import os\nimport rpy2.rinterface as rinterface\n\n# IMPORTANT: rpy2-rinterface requires a functional R installation on your system.\n# If R is not in your system's PATH, you might need to set R_HOME:\n# os.environ['R_HOME'] = '/path/to/R'\n# os.environ['PATH'] += os.pathsep + os.path.join(os.environ['R_HOME'], 'bin')\n\ntry:\n    # Initialize the R interpreter\n    # This must be called before interacting with R.\n    rinterface.initr()\n    print(\"R interpreter initialized successfully.\")\n\n    # Access R's base environment\n    base = rinterface.baseenv\n\n    # Evaluate R code directly as a string\n    r_code = 'x <- 1:10; mean(x)'\n    result = rinterface.parse(r_code).eval(base)\n    print(f\"R evaluation (mean of 1:10): {result[0]} (R type: {result.rid})\")\n\n    # Call an R function with Python data\n    r_sum_func = base['sum']\n    python_list = [10, 20, 30]\n    r_vector = rinterface.IntSexpVector(python_list)\n    sum_result = r_sum_func(r_vector)\n    print(f\"Sum of {python_list} in R: {sum_result[0]}\")\n\nexcept rinterface.RRuntimeError as e:\n    print(f\"R Runtime Error: {e}\\nEnsure R is installed and R_HOME is correctly set if needed.\")\nexcept Exception as e:\n    print(f\"An unexpected error occurred: {e}\")","lang":"python","description":"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)."},"warnings":[{"fix":"Instead 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.","message":"The `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.","severity":"breaking","affected_versions":">=3.6.0"},{"fix":"Install 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.","message":"rpy2-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.","severity":"gotcha","affected_versions":"All"},{"fix":"If encountering loading errors on Windows, try setting `os.environ['RPY2_CFFI_MODE'] = 'API'` before importing `rpy2.rinterface`. The default is `EXT`.","message":"On 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.","severity":"gotcha","affected_versions":">=3.6.5 (API mode working on Windows)"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Ensure R is installed. Set the `R_HOME` environment variable to the root of your R installation (e.g., `os.environ['R_HOME'] = '/path/to/R'`) before importing `rpy2.rinterface`.","cause":"The rpy2 library cannot find the R installation directory on your system.","error":"RuntimeError: R_HOME is not set."},{"fix":"Make sure your R installation's `bin` directory (e.g., `R_HOME/bin/x64`) is included in your system's PATH environment variable. For Windows, ensure the correct architecture (32-bit vs 64-bit) matches your Python installation.","cause":"The Python interpreter cannot find the R shared library (e.g., R.dll on Windows, libR.so on Linux, libR.dylib on macOS) or its dependencies.","error":"OSError: cannot load library 'R' / DLL load failed while importing _rinterface_cffi_ext"},{"fix":"Update your code to use the new initialization pattern: `import rpy2.rinterface as rinterface; rinterface.initr()`. Access global R environments via `rinterface.baseenv` and `rinterface.globalenv`.","cause":"You are attempting to use the `R` class to access the R interpreter, which was removed in `rpy2` version 3.6.0.","error":"AttributeError: module 'rpy2.rinterface' has no attribute 'R'"}]}