RPweave: Unified R + Python + LaTeX Document System

The Problem: Language Fragmentation in Research

As data scientists and researchers, we often find ourselves using three worlds:

  • R for statistical analysis and beautiful visualizations (e.g ggplot2 or esgtoolkit)
  • Python for machine learning and broader ecosystem (scikit-learn, PyTorch)
  • LaTeX for professional typesetting and academic publishing

Traditionally (a long time ago, though), this meant maintaining separate scripts, manually copying results, and struggling with reproducible workflows.

There are existing solutions like R Markdown and Jupyter Notebooks,and Quarto, but none of them fully address my needs for reproducibility and environment management using any LaTeX template.

So, I hacked a workflow with RPweave – a unified document system that brings together the best of all worlds. It relies on:

  • Knitr for R and Python code chunks
  • Reticulate for Python integration in R
  • LaTeX for high-quality typesetting
  • uv for Python environment management
  • Makefile/command line for automated builds (see below, as it’s especially designed for command-line use)
  • Git template for version control (https://github.com/Techtonique/RPweave)

Getting Started

# 1. Use the GitHub template
git clone https://github.com/Techtonique/RPweave my-paper
cd my-paper

# 2. Setup environments
uv venv venv
source venv/bin/activate
make setup

# 3. Start writing!
make view

More on RPweave

Example document structure:

\documentclass{article}
\begin{document}

%mandatory Rnw setup
<<setup>>=
library(knitr)
library(reticulate)
use_python("venv/bin/python")
@

\section{R Analysis}
<<r-analysis>>=
ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()
@

\section{Python Analysis}  
<<python-analysis, engine='python'>>=
import pandas as pd
df = pd.DataFrame({'x': range(100)})
print(df.describe())
@

\end{document}

Key Features

ENVIRONMENT MANAGEMENT

  • Python: Managed by uv - the fast, modern Python package manager
  • R: Simple, conditional package installation
  • Isolated: Virtual environments for reproducible builds

SEAMLESS CROSS-LANGUAGE INTEGRATION

# R to Python data sharing
r_data <- data.frame(x=1:10, y=rnorm(10))
py$shared_data <- r_data  # Available in Python chunks!

PROFESSIONAL LATEX OUTPUT

  • Academic-quality typesetting
  • Automatic figure generation and placement
  • Code syntax highlighting
  • Bibliography support

WORKFLOW

# One-time setup
## create and activate Python virtual environment
uv venv venv && source venv/bin/activate
## install R and Python dependencies
make setup

# Daily development  
make view  # Build and open PDF after edits in main.Rnw

Project Structure

research-paper/
├── main.Rnw          # Your paper with embedded R/Python
├── chunks/           # Modular code organization
├── data/             # Research datasets  
├── outputs/          # Generated figures
├── requirements.txt  # Python dependencies
└── Makefile          # Automated build system

Real-World Use Case

Imagine writing a paper that needs:

  1. Complex statistical models (R: lme4, brms)
  2. Deep learning analysis (Python: PyTorch, TensorFlow)
  3. Professional formatting (LaTeX: equations, algorithms)
  4. Reproducible results (isolated environments)

With RPweave, you get all of this in a single, version-controlled document.

Pro Tips from Production Use

  1. Use make view as your primary writing command - it’s like “Save & Preview”
  2. Organize long code in named chunks/ directory to keep main.Rnw clean
  3. Version control everything except generated files (use .gitignore)
  4. R→Python data flow works seamlessly - structure analyses accordingly

The Expected Result for Me

  • No more context switching between RStudio and Jupyter for LaTeX writing
  • Integrated workflow that saves time and reduces errors
  • Single source of truth for analysis and manuscript
  • Professional output that meets academic standards
  • Truly reproducible research with pinned dependencies (but don’t forget to git commit)

image-title-here

Comments powered by Talkyard.