Site icon R-bloggers

RPweave: Unified R + Python + LaTeX System using uv

[This article was first published on T. Moudiki's Webpage - R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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:

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:

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

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

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

Links & Resources

To leave a comment for the author, please follow the link and comment on their blog: T. Moudiki's Webpage - R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Exit mobile version