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
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.
- rpy2.robjects: Interface to R objects
- Pandas Integration: Conversion utilities between Pandas and R data frames
- Graphics Module: Working with R graphics in Python
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.
Pandas and R DataFrame Integration
Learn how to convert between Pandas DataFrames and R data frames seamlessly.
Calling R from Python
Practical examples showing how to leverage R functionality in Python statistical analysis.
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
Stack Overflow
Community questions and answers related to rpy2 implementation challenges.
Q&AR-bloggers
Collection of blog posts discussing rpy2 applications and use cases.
ArticlesTwitter #rpy2
Stay updated with community discussions and announcements related to rpy2.
SocialGitHub Discussions
Ask questions, share ideas, and connect with other rpy2 users.
ForumGitHub Stars
1.2k+
PyPI Downloads
500k+
Contributors
50+
Advanced Topics
- Extending rpy2 with Custom Interfaces: Learn how to create custom interfaces for R packages.
- Working with R Graphics in Python: Guide to using R's powerful plotting capabilities (like ggplot2) within Python.
- Performance Considerations: Tips for optimizing rpy2 code and understanding memory management between Python and R.
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
Performance Improvements
Optimized data conversion between large Pandas DataFrames and R data.frames
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