CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-rpy2

Python interface to the R language (embedded R)

Pending
Overview
Eval results
Files

jupyter-integration.mddocs/

Jupyter Integration

IPython magic commands and rich display integration for seamless R usage in Jupyter notebooks with automatic plot rendering, data visualization, and interactive R computing.

Capabilities

IPython Extension Loading

Load rpy2 extension to enable R magic commands in Jupyter notebooks and IPython environments.

def load_ipython_extension(ipython):
    """
    Load rpy2 IPython extension.
    
    Parameters:
    - ipython: IPython instance
    
    Enables %R line magic and %%R cell magic commands
    for executing R code within Python notebooks.
    
    Usage:
    %load_ext rpy2.ipython
    """

Magic Commands

Execute R code directly in Jupyter notebook cells with automatic data exchange and visualization.

# Line magic: %R
# Execute single line of R code
# Usage: %R r_code_here

# Cell magic: %%R  
# Execute entire cell as R code
# Usage:
# %%R [options]
# r_code_line_1
# r_code_line_2
# ...

# Magic command options:
# -i INPUT: Input Python variable to R
# -o OUTPUT: Output R variable to Python  
# -w WIDTH: Plot width in pixels
# -h HEIGHT: Plot height in pixels
# -r RESOLUTION: Plot resolution in DPI
# --units UNITS: Plot size units ('in', 'px', 'cm')

HTML Display Integration

Rich HTML output and visualization rendering for R graphics and data in Jupyter notebooks.

# Available in rpy2.ipython.html module
class HTMLResult:
    """
    HTML representation of R objects for Jupyter display.
    
    Automatically renders R objects as HTML in notebook
    cells for rich interactive display.
    """
    def _repr_html_(self) -> str: ...

def display_html(r_object):
    """
    Display R object as HTML in Jupyter notebook.
    
    Parameters:
    - r_object: R object to display
    
    Automatically formats R data frames, summaries,
    and other objects for rich notebook display.
    """

ggplot2 Integration

Specialized integration for ggplot2 graphics with automatic rendering and interactive display.

# Available in rpy2.ipython.ggplot module
class GGPlot:
    """
    ggplot2 plot wrapper with Jupyter display integration.
    
    Automatically renders ggplot2 plots in notebook cells
    with proper sizing, resolution, and format handling.
    """
    def _repr_png_(self) -> bytes: ...
    def _repr_svg_(self) -> str: ...
    def show(self, width=None, height=None, dpi=None): ...

def activate_ggplot():
    """
    Activate automatic ggplot2 rendering in Jupyter.
    
    Enables automatic display of ggplot2 objects
    when they are the last expression in a cell.
    """

def deactivate_ggplot():
    """
    Deactivate automatic ggplot2 rendering.
    
    Returns to default R graphics behavior
    without automatic Jupyter integration.
    """

Data Exchange

Seamless data transfer between Python and R variables in notebook environments.

# Data flows automatically between Python and R
# Python → R: Use -i option or direct assignment
# R → Python: Use -o option or direct access

# Automatic conversions:
# pandas DataFrame ↔ R data.frame
# NumPy array ↔ R vector/matrix  
# Python list ↔ R vector
# Python dict ↔ R named list

Magic Command Configuration

Configure magic command behavior and default settings.

# IPython magic configuration options
class RMagicsConfig:
    """Configuration for R magic commands."""
    cache_display_data: bool = True    # Cache plot data
    device: str = 'png'               # Default graphics device
    units: str = 'in'                 # Size units
    res: int = 72                     # Default resolution
    width: float = 7.0                # Default width  
    height: float = 7.0               # Default height
    pointsize: int = 12               # Default point size

Usage Examples

# Load the extension (run this once per notebook)
%load_ext rpy2.ipython

# Basic R execution
%R print("Hello from R!")

# Execute R code and capture output
%R x <- c(1, 2, 3, 4, 5)
%R mean_x <- mean(x)  
%R print(mean_x)

# Transfer data from Python to R
import pandas as pd
import numpy as np

python_data = pd.DataFrame({
    'A': [1, 2, 3, 4, 5],
    'B': [1.1, 2.2, 3.3, 4.4, 5.5]
})

%R -i python_data
%R print(head(python_data))
%R summary(python_data)

# Transfer data from R to Python
%R r_result <- lm(B ~ A, data=python_data)
%R -o r_result
print(f"R result in Python: {type(r_result)}")

# Cell magic for multi-line R code
%%R
# This entire cell is executed as R code
library(ggplot2)

# Create a plot
p <- ggplot(python_data, aes(x=A, y=B)) +
  geom_point() +
  geom_smooth(method="lm") +
  labs(title="Linear Regression Plot",
       x="Variable A", 
       y="Variable B")

print(p)

# Control plot size and resolution
%%R -w 800 -h 600 -r 150
library(ggplot2)
ggplot(mtcars, aes(x=wt, y=mpg)) +
  geom_point() +
  geom_smooth() +
  labs(title="Car Weight vs MPG")

# Bidirectional data flow
%%R -i python_data -o r_summary
# Input python_data from Python, output r_summary to Python
r_summary <- summary(lm(B ~ A, data=python_data))
print(r_summary)

# Now r_summary is available in Python
print(f"R summary type: {type(r_summary)}")

# Working with multiple variables
x_data = np.array([1, 2, 3, 4, 5])
y_data = np.array([2, 4, 6, 8, 10])

%%R -i x_data,y_data -o correlation,p_value
# Multiple inputs and outputs
correlation <- cor(x_data, y_data)  
test_result <- cor.test(x_data, y_data)
p_value <- test_result$p.value

print(paste("Correlation:", correlation))
print(paste("P-value:", p_value))

print(f"Correlation: {correlation[0]}")
print(f"P-value: {p_value[0]}")

Advanced Integration Patterns

# Custom plot styling and output
%%R -w 1000 -h 800 --units px -r 300
library(ggplot2)
library(dplyr)

# Advanced ggplot with custom styling
mtcars %>%
  ggplot(aes(x=wt, y=mpg, color=factor(cyl))) +
  geom_point(size=3, alpha=0.7) +
  geom_smooth(method="lm", se=FALSE) +
  facet_wrap(~gear) +
  labs(title="MPG vs Weight by Cylinders and Gears",
       x="Weight (1000 lbs)",
       y="Miles Per Gallon",
       color="Cylinders") +
  theme_minimal() +
  theme(plot.title = element_text(hjust = 0.5, size=16))

# Statistical analysis with output capture
import scipy.stats as stats

sample_data = np.random.normal(100, 15, 100)

%%R -i sample_data -o r_stats
# Comprehensive statistical analysis
r_stats <- list(
  mean = mean(sample_data),
  median = median(sample_data),
  sd = sd(sample_data),
  shapiro_test = shapiro.test(sample_data),
  normality_p = shapiro.test(sample_data)$p.value
)

print("Statistical Summary:")
print(r_stats)

# Access detailed results in Python
print(f"Mean: {r_stats[0][0]:.2f}")
print(f"Median: {r_stats[1][0]:.2f}")  
print(f"Standard Deviation: {r_stats[2][0]:.2f}")
print(f"Normality test p-value: {r_stats[4][0]:.4f}")

# Interactive plotting with multiple datasets
datasets = {
    'iris': pd.read_csv('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv'),
    'tips': pd.read_csv('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/tips.csv')
}

for name, data in datasets.items():
    print(f"\n=== Analysis of {name} dataset ===")
    
    %R -i data
    %%R
    print(paste("Dataset shape:", nrow(data), "x", ncol(data)))
    print("Summary:")
    print(summary(data))
    
    if name == 'iris':
        %%R -w 600 -h 400
        library(ggplot2)
        ggplot(data, aes(x=sepal_length, y=sepal_width, color=species)) +
          geom_point() +
          labs(title="Iris: Sepal Length vs Width")
    
    elif name == 'tips':
        %%R -w 600 -h 400  
        library(ggplot2)
        ggplot(data, aes(x=total_bill, y=tip, color=time)) +
          geom_point() +
          geom_smooth(method="lm") +
          labs(title="Tips: Total Bill vs Tip Amount")

Configuration and Troubleshooting

# Check if extension is loaded
%lsmagic  # Should show %R and %%R in line/cell magics

# Reload extension if needed
%reload_ext rpy2.ipython

# Configure default plot settings
%config InlineBackend.figure_format = 'retina'  # High-res plots
%config InlineBackend.rc = {'figure.figsize': (10, 6)}

# Debug R magic issues
%R print(R.version.string)  # Check R version
%R print(.libPaths())       # Check R library paths
%R print(sessionInfo())     # Complete R session info

# Handle R warnings and errors
%%R
options(warn=1)  # Print warnings immediately
print("R is working correctly")

# Memory management for large datasets
%%R
# Clean up R environment
rm(list=ls())
gc()  # Garbage collection

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