{"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.","language":"python","status":"active","last_verified":"Fri Apr 17","install":{"commands":["pip install rpy2-rinterface"],"cli":null},"imports":["from rpy2.rinterface import initr","from rpy2.rinterface import baseenv","from rpy2.rinterface import evalr"],"auth":{"required":false,"env_vars":[]},"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).","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}