{"id":6863,"library":"rpy2","title":"rpy2: Python interface to the R language","description":"rpy2 is a powerful Python package that provides a bridge between Python and the R programming language. It enables seamless integration of R's statistical capabilities and specialized packages with Python's versatile ecosystem. The library is actively developed, with its current stable version being 3.6.7, and maintains a continuous development and release cadence.","status":"active","version":"3.6.7","language":"en","source_language":"en","source_url":"https://github.com/rpy2/rpy2","tags":["R","data science","interoperability","statistical computing","data frames","scientific computing"],"install":[{"cmd":"pip install rpy2","lang":"bash","label":"Install rpy2"},{"cmd":"# System requirements (install R first)\nsudo apt-get install r-base r-base-dev # Debian/Ubuntu\nbrew install r # macOS","lang":"bash","label":"Install R (system-level dependency)"}],"dependencies":[{"reason":"rpy2 requires Python >=3.9.","package":"python","optional":false},{"reason":"rpy2 interfaces with the R language, requiring R 4.0 or higher to be installed and accessible on the system PATH or via R_HOME environment variable.","package":"R","optional":false},{"reason":"Commonly used for seamless conversion between Python DataFrames and R data.frames.","package":"pandas","optional":true},{"reason":"Commonly used for conversion between Python arrays and R vectors/matrices.","package":"numpy","optional":true}],"imports":[{"note":"`robjects` is the high-level interface for R objects; `rpy2.r` is less common for direct interaction.","wrong":"import rpy2.r as r","symbol":"robjects","correct":"import rpy2.robjects as robjects"},{"note":"Used to import R packages into Python.","symbol":"importr","correct":"from rpy2.robjects.packages import importr"},{"note":"Module for converting between pandas DataFrames and R data.frames.","symbol":"pandas2ri","correct":"from rpy2.robjects import pandas2ri"}],"quickstart":{"code":"import rpy2.robjects as ro\nfrom rpy2.robjects.packages import importr\nfrom rpy2.robjects import pandas2ri\nimport pandas as pd\n\n# Activate conversion between pandas and R (optional, but common)\npandas2ri.activate()\n\n# Import an R package (e.g., base R's 'stats' for statistical functions)\nstats = importr('stats')\nbase = importr('base')\n\n# Define R code as a Python string and execute it\nro.r('''\n    # Generate a sequence of numbers\n    x <- 1:10\n    # Calculate the mean\n    mean_x <- mean(x)\n    # Create an R data frame\n    r_df <- data.frame(A=c(1,2,3), B=c('x','y','z'))\n''')\n\n# Access R variables from Python\nmean_x = ro.r['mean_x'][0]\nprint(f\"Mean of x from R: {mean_x}\")\n\n# Get an R data frame and convert it to pandas (if pandas2ri is activated)\nr_dataframe = ro.r['r_df']\npy_dataframe = pandas2ri.rpy2py(r_dataframe)\nprint(\"Python DataFrame from R:\")\nprint(py_dataframe)\nprint(f\"Type of py_dataframe: {type(py_dataframe)}\")\n\n# Example: Call an R function with Python objects\npy_data = pd.DataFrame({'value': [10, 20, 30], 'group': ['A', 'B', 'A']})\nr_data = pandas2ri.py2rpy(py_data)\n\n# Use an R function (e.g., 't.test') from the 'stats' package\nt_test_result = stats.t_test(ro.Formula('value ~ group'), data=r_data)\nprint(\"\\nT-test result from R:\")\nprint(t_test_result)","lang":"python","description":"This quickstart demonstrates how to initialize rpy2, activate automatic conversion between pandas DataFrames and R data.frames, execute arbitrary R code, access R objects from Python, and call R functions with Python data structures. It showcases the core `rpy2.robjects` and `rpy2.robjects.pandas2ri` modules."},"warnings":[{"fix":"Ensure R is correctly installed and its `bin` directory is in your system's PATH. Alternatively, set the R_HOME environment variable to your R installation directory. On Windows, explicitly setting `RPY2_CFFI_MODE=API` might be necessary for some functionalities after R 3.6.5.","message":"rpy2 requires a working R installation. Issues often arise from R not being in the system's PATH or the R_HOME environment variable not being set, leading to `OSError: cannot load library` on import.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Refer to the official rpy2 documentation for the specific version you are upgrading to. Pay close attention to migration guides, especially for the `rpy2.rinterface` and `rpy2.robjects` modules, and how R objects are represented in Python.","message":"Significant architectural and API changes occurred between major versions (e.g., RPy-1.x to rpy2, and further changes in rpy2 3.x, including a shift to `cffi` for its R interface). Code written for older versions will likely break.","severity":"breaking","affected_versions":"< 3.0.0"},{"fix":"Use `rpy2.robjects.packages.importr('utils').install_packages('your_package')` from within Python, or ensure that the R packages are installed in a location Rpy2 can find, typically in R's default library paths. Check `base._libPaths()` to see R's active library paths.","message":"R packages (e.g., ggplot2, dplyr) must be installed within R or explicitly managed by rpy2 for `importr` to find them. Simply installing them in R might not make them immediately visible to rpy2 if R's library paths are not correctly configured or unique for the rpy2 environment.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For fine-grained control, avoid `pandas2ri.activate()` and instead use `pandas2ri.py2rpy()` and `pandas2ri.rpy2py()` for explicit conversions within specific contexts or with custom converters.","message":"While `pandas2ri.activate()` provides convenient automatic conversion between pandas and R data structures, it's a global setting that can sometimes lead to unexpected behavior or conflicts. For more controlled conversions, consider using explicit converter objects.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Wrap R code execution in `try-except rpy2.rinterface.RRuntimeError` blocks. For persistent crashes, inspect the R code for deeper C++-level issues or consult R's error messages for diagnostics.","message":"Errors originating from R code executed via rpy2 are typically raised as `rpy2.rinterface.RRuntimeError`. However, some low-level R errors (e.g., from R's C++ components) can lead to Python crashing with a core dump rather than raising a catchable exception.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-15T00:00:00.000Z","next_check":"2026-07-14T00:00:00.000Z","problems":[]}