CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-rpy2

Python interface to the R language (embedded R)

Pending
Overview
Eval results
Files

high-level-interface.mddocs/

High-Level R Interface

The high-level interface (rpy2.robjects) provides the primary entry point for most rpy2 users, offering Pythonic access to R objects, functions, and data structures with automatic type conversion.

Capabilities

R Session Access

The global r object provides direct access to the embedded R session, allowing execution of R code and access to R objects.

class R:
    def __call__(self, string: str, invisible: bool = None, print_r_warnings: bool = None) -> Any:
        """
        Execute R code string and return result.
        
        Parameters:
        - string: R code to execute
        - invisible: If True, suppress R output
        - print_r_warnings: If True, print R warnings
        
        Returns:
        R object converted to Python equivalent
        """
    
    def __getitem__(self, item: str) -> Any:
        """
        Get R object by name from global environment.
        
        Parameters:
        - item: Name of R object
        
        Returns:
        R object converted to Python wrapper
        """
    
    def __getattribute__(self, attr: str) -> Any:
        """
        Access R object as Python attribute.
        
        Parameters:
        - attr: R object name
        
        Returns:
        R object as Python attribute
        """

# Global R session singleton
r: R

R Code Evaluation

Direct evaluation of R code with high-level type conversion.

def reval(string: str, envir = globalenv) -> Any:
    """
    Evaluate R code string with robjects conversion.
    
    Parameters:
    - string: R code to evaluate
    - envir: R environment for evaluation (default: global environment)
    
    Returns:
    Evaluated result with robjects conversion
    """

Base R Object Wrappers

High-level wrappers for core R objects with Python-like interfaces.

class RObject:
    """Base mixin class for R objects with high-level interface."""
    
class ExternalPointer(RObject):
    """Wrapper for R external pointer objects."""
    
class Formula(RObject):
    """
    R formula object wrapper with environment management.
    
    Represents R formulas like 'y ~ x1 + x2' for statistical modeling.
    """

Function Interface

Wrappers for R functions that provide Python-like calling conventions.

class Function(RObject):
    """
    R function wrapper with Python callable interface.
    
    Allows calling R functions from Python with automatic
    argument conversion and Python-style parameter passing.
    """
    def __call__(self, *args, **kwargs) -> Any: ...

class SignatureTranslatedFunction(Function):
    """
    Function with Python-like signature translation.
    
    Provides more Pythonic parameter names and calling conventions
    by translating R parameter names to Python conventions.
    """

class DocumentedSTFunction(SignatureTranslatedFunction):
    """
    Documented signature-translated function with help text.
    
    Includes R help documentation accessible from Python.
    """

Language Object Interface

Wrappers for R language objects and unevaluated expressions.

class LangVector(RObject):
    """
    R language object wrapper for unevaluated R expressions.
    
    Represents R calls, expressions, and language constructs
    that can be evaluated in R contexts.
    """
    
    @classmethod
    def from_string(cls, string: str) -> 'LangVector':
        """
        Create language vector from R code string.
        
        Parameters:
        - string: R code as string
        
        Returns:
        Language vector representing the R expression
        """

Usage Examples

import rpy2.robjects as robjects
from rpy2.robjects import r

# Execute R code directly
result = r('2 + 2')
print(result[0])  # 4.0

# Get R objects by name
r('x <- c(1, 2, 3, 4, 5)')
x = r['x']
print(list(x))  # [1.0, 2.0, 3.0, 4.0, 5.0]

# Access R objects as attributes
r('y <- mean(c(1, 2, 3, 4, 5))')
y = r.y
print(y[0])  # 3.0

# Work with R functions
r_sum = r['sum']
result = r_sum(robjects.IntVector([1, 2, 3, 4, 5]))
print(result[0])  # 15.0

# Create and use formulas
from rpy2.robjects import Formula
formula = Formula('y ~ x1 + x2')
print(formula)

# Use high-level evaluation
result = robjects.reval('mean(c(1, 2, 3, 4, 5))')
print(result[0])  # 3.0

Install with Tessl CLI

npx tessl i tessl/pypi-rpy2

docs

environment-management.md

high-level-interface.md

index.md

jupyter-integration.md

low-level-interface.md

package-management.md

type-conversion.md

vectors-datatypes.md

tile.json