Welcome to the rpy2 Documentation

Explore the powerful integration of Python and R with rpy2, enabling seamless data analysis and visualization workflows.

What is rpy2?

rpy2 is a powerful Python package that provides a bridge between Python and the R programming language. It allows data scientists and researchers to seamlessly integrate R's statistical capabilities and specialized packages with Python's versatile ecosystem, enabling the best of both worlds in a single workflow.

Direct R Interface

Call R functions directly from Python and access R objects with native Python syntax.

Data Conversion

Seamlessly convert between R and Python data structures (DataFrames, vectors, etc).

Graphics Support

Use R's powerful plotting capabilities like ggplot2 from within Python.

Active Ecosystem

Benefit from continuous development and an active community of users.

Installation

Install rpy2 easily using pip:

pip install rpy2

System Requirements

  • Python 3.7 or higher
  • R 3.6.0 or higher
  • Development tools (gcc/clang)
  • Required system libraries

Install system dependencies first:

sudo apt-get install r-base r-base-dev

Using Homebrew:

brew install r

Install R from CRAN and ensure R's bin directory is in your PATH.

API Reference

Explore the comprehensive API documentation to understand the available classes, methods, and functions.

Quick Reference

rpy2.robjects
  • r (Function)
  • FloatVector (Class)
  • IntVector (Class)
  • StrVector (Class)
  • DataFrame (Class)
rpy2.robjects.packages
  • importr() (Function)
  • data() (Function)
  • Package (Class)
rpy2.robjects.pandas2ri
  • activate() (Function)
  • py2rpy() (Function)
  • rpy2py() (Function)

Tutorials and Guides

Getting Started with rpy2

A beginner-friendly guide to setting up rpy2 and understanding its core components.

15 min read
Read Tutorial

Pandas and R DataFrame Integration

Learn how to convert between Pandas DataFrames and R data frames seamlessly.

25 min read
Read Tutorial

Calling R from Python

Practical examples showing how to leverage R functionality in Python statistical analysis.

20 min read
Read Tutorial

Code Examples

Learn by example with these practical code snippets demonstrating rpy2 functionality.

import rpy2.robjects as robjects

# Execute R code
robjects.r('''
    x <- rnorm(100)
    mean_x <- mean(x)
    sd_x <- sd(x)
''')

# Access R variables from Python
mean_x = robjects.r['mean_x']
sd_x = robjects.r['sd_x']

print(f"Mean: {mean_x[0]}, Standard Deviation: {sd_x[0]}")
import pandas as pd
import rpy2.robjects as ro
from rpy2.robjects import pandas2ri
from rpy2.robjects.packages import importr

# Activate conversion between pandas and R
pandas2ri.activate()

# Create pandas DataFrame
pdf = pd.DataFrame({
    'a': [1, 2, 3, 4, 5],
    'b': ['a', 'b', 'c', 'd', 'e'],
    'c': [True, False, True, False, True]
})

# Convert to R DataFrame
rdf = pandas2ri.py2rpy(pdf)

# Use R functions on the dataframe
stats = importr('stats')
result = stats.aggregate(rdf.rx2('a'), rdf.rx2('c'), ro.r('mean'))

# Convert result back to Python
py_result = pandas2ri.rpy2py(result)
import rpy2.robjects as ro
from rpy2.robjects.packages import importr
from rpy2.robjects import pandas2ri
import pandas as pd

# Activate pandas to R conversion
pandas2ri.activate()

# Import ggplot2 and other packages
ggplot2 = importr('ggplot2')
datasets = importr('datasets')

# Get mtcars dataset
mtcars = ro.r('mtcars')
# For Python object
mtcars_pd = pandas2ri.rpy2py(mtcars)

# Create a ggplot
pp = ggplot2.ggplot(mtcars) + \
     ggplot2.aes_string(x='wt', y='mpg', color='factor(cyl)') + \
     ggplot2.geom_point() + \
     ggplot2.ggtitle('MPG vs Weight by Cylinders')

# Display the plot
ro.r.X11()
pp.plot()
input("Press Enter to continue...")

Community Resources

GitHub Stars

1.2k+

PyPI Downloads

500k+

Contributors

50+

Advanced Topics

Memory Management Between Python and R

Understanding how rpy2 manages memory between Python and R is crucial for optimal performance. This section explains reference counting, garbage collection, and best practices.

Learn more β†’
Custom Conversion Between Data Types

Create custom converters for specialized data types or when working with specific R packages that require special handling.

Learn more β†’
Threading and Parallel Processing

Learn about rpy2's thread safety considerations and how to effectively implement parallel processing across Python and R.

Learn more β†’

Recent Updates

April 2025

Version 3.5.17 Released

Added support for Python 3.12 and R 4.3.x

View release notes
March 2025

Performance Improvements

Optimized data conversion between large Pandas DataFrames and R data.frames

February 2025

New Documentation

Complete overhaul of documentation with new examples and improved navigation

Upcoming Features

  • Improved support for R's tidyverse ecosystem
  • Better integration with Jupyter notebooks
  • Enhanced error handling and debugging capabilities