Week 8 · Tuesday, Nov 17 · Lecture
| Time | What |
|---|---|
| 1:30 | Runbook render: your questions from Thursday |
| 1:40 | Why put two languages in one document |
| 1:45 | How it works: knitr + reticulate |
| 2:00 | Passing data between R and Python |
| 2:20 | renv: the packages are part of the project |
| 2:30 | Workshop: your own runbook |
| 3:00 | Before Thursday |
Learning goals: run Python chunks inside a Quarto document next to R; move a data frame from R to Python and back, checking what changed on the way; restore and update a project’s package versions with renv; get your own project to render from a runbook.
No AI in today’s exercises. In the workshop you’re working on your project, where AI is allowed with disclosure.
⏱ 10 min
Open time, not graded. Bring the problems from the “Runbook render” and “Break it” Ed threads.
If nobody starts: why does renv::restore() install packages you already have? Why did the first render take longer than the second? What is execute-dir: project for? Why are data/processed/ and output/ empty after a fresh clone?
One document, one session, one render: the Python step is reproducible too.
The alternative, “run this Python script first, then render”, is a step a reader will forget.
```{python} runs Pythonpy_require() declares the Python packages. The first render downloads Python + pandas into a cache; nobody installs Python by handA .qmd with only Python chunks uses the jupyter engine instead. Same chunks, different engine.
| From | To | Write | Example |
|---|---|---|---|
| R | Python | r. |
r.analysis in a {python} chunk |
| Python | R | py$ |
py$correlations in an {r} chunk |
Read n_countries before the correlation. A correlation of −1 from two countries tells you nothing.
reticulate converts types for you. Usually:
| R | Python |
|---|---|
data.frame / tibble |
pandas DataFrame |
| numeric vector, length > 1 | NumPy array (in a data frame: a column) |
| length-1 vector | a single number or string |
| named list | dict |
NA in a numeric column |
NaN |
| factor | pandas Categorical |
Check row counts and missing values on both sides.
Same habit as after a join: nrow(analysis) in R, len(r.analysis) and r.analysis.isna().sum() in Python. Also: Python counts from 0. The first row is df.iloc[0].
⏱ 15 min
In the example runbook, after the py-correlations chunk:
{python} chunk that counts countries per income group in 2007, from r.analysis.df[df["year"] == 2007].groupby("income_group").size()r.analysis have in Python? Is it the same as nrow(analysis) in R?{r} chunk, look at py$.... What type did it become? (class())reset_index(), .size() returns a pandas Series, which comes back to R as a named array (class() says "array"), not a data framereset_index(name = ...) gives a DataFrame, which comes back as a data frame`r py$by_income$income_group[which.max(py$by_income$n_countries)]`renvYour analysis depends on versions. A dplyr update can change a default; a render that worked in November can fail in March.
| You want to… | Run | What it does |
|---|---|---|
| start using renv in your project | renv::init() |
Project library + renv.lock + .Rprofile |
get the versions in renv.lock (after cloning) |
renv::restore() |
Installs exactly those versions |
| record a package you just added | renv::snapshot() |
Updates renv.lock. Commit it |
| see what’s out of sync | renv::status() |
Lists used-but-not-recorded packages |
Commit renv.lock, .Rprofile, renv/activate.R and renv/settings.json. Don’t commit renv/library/ (renv’s own .gitignore handles it). Python packages are recorded by py_require(), in the runbook itself.
run-analysis.qmd⏱ 30 min
template/run-analysis-template.qmd from the example repo into your project’s top folder, as run-analysis.qmdR/01-...R, R/02-...R, and source() it from a chunk. Render after each steprenv::init(), render, commitYour mid-quarter .qmd is not wasted: its code goes into the scripts, its text goes into the runbook. AI is allowed here with disclosure; note it in your “AI use” section as you go. I’ll come around.
{python} chunks run next to {r} chunks; with any R chunk, Quarto uses knitr + reticulate, and both share one sessionr.x reads R objects in Python; py$x reads Python objects in RNA/NaN on both sides; a pandas Series comes back as a named array, not a data framepy_require() puts the Python dependencies in the documentrenv: init() once, restore() after cloning, snapshot() after adding a package, and commit renv.locksource() the scripts, render after each changeThursday, Nov 19: writing good software, using AI with rules, and AI ethics. Tuesday, Nov 24: peer code review, in class, on your repository. It must render from a fresh clone by then.

STAT 266A · Fall 2026 · UC Santa Cruz