6  Keeping Exploration Notes, and Where Python Fits

This chapter covers how to keep analysis/ notebooks usable over time, and the rules for using both R and Python.

6.1 Old notebooks stop working

In Chapter 2 we moved decided processing into functions in scripts/. That leads to situations like this:

  • A notebook written in November calls fit_main_model(cohort)
  • In December you add an argument to fit_main_model() or rename it
  • You open the November notebook, run it, and it errors or gives different results

Exploration notebooks may be disposable, but sometimes you want to see “the result I showed my supervisor back then” again.

6.2 Remedy 1: keep results with freeze

Write freeze: auto in analysis/_metadata.yml.

analysis/_metadata.yml (excerpt)
execute:
  # On a whole-project render (quarto render), qmd files that haven't changed are not re-executed
  #   → results from a previous render stay in _freeze/
  freeze: auto
  echo: false
  • When you render, the computed results are saved to _freeze/analysis/<notebook name>/
  • When you run quarto render on the whole project, notebooks whose qmd has not changed are not re-executed; the saved results are used as they are
  • Even if functions in scripts/ change and an old notebook no longer runs, its results are kept
NoteRendering a single file always re-executes it

freeze only takes effect for “whole-project renders”. Rendering just one notebook (e.g. with \qp in nvim) always re-executes it. Commit _freeze/ to git.

6.3 Remedy 2: note the commit at the time of writing

At the top of each notebook, paste the git commit at the time it was written.

<!-- commit at time of writing: a1b2c3d → if it stops working, git checkout this first -->

You can get the commit with:

git log -1 --format=%h
#> a1b2c3d

If you really need to run it in the state of that time, check out that commit and run it.

git stash            # temporarily shelve current changes
git checkout a1b2c3d
# ... run the notebook and check ...
git checkout main
git stash pop

6.4 Naming notebooks

  • Start with the date: 2026-11-20_model_explore.qmd
  • After the date, briefly say what you did: _model_explore, _missing_check, _sensitivity_smoking
  • To continue the same theme, create a new notebook with a new date (don’t overwrite old ones)

The file list is automatically sorted chronologically, so it becomes a diary of “what was tried, and when”.

6.5 Using reports/

Materials for your supervisor or collaborators are kept in reports/, separate from analysis/ notebooks.

  • They contain only what needs to be shown among the findings from analysis/
  • For results, read the same ones as the paper with targets::tar_read(), or call functions in scripts/
  • For slides, Quarto’s revealjs or pptx formats work (Chapter 7)

6.6 Division of labour between R and Python

When you use both R and Python, it is tempting to “write the same model in both R and Python”. But that is exactly the double maintenance we avoided in Chapter 2. You fix only one side, and R and Python drift apart.

ImportantRule: finalized processing in one language only

In this project, the processing that goes into the paper (scripts/ and targets) is R only. Python is used for exploration in analysis/.

6.6.1 Exploring in Python

The sample includes a Python exploration notebook, analysis/2026-11-20_model_explore_py.qmd.

analysis/2026-11-20_model_explore_py.qmd (excerpt)
```{python}
#| label: setup
from scripts.common.utils import *          # shared settings (packages, paths)
import statsmodels.formula.api as smf

cohort = pd.read_parquet(PROCESSED_DIR / "cohort.parquet")
cohort["sarcopenia"] = (cohort["sarcopenia"] == "Sarcopenia").astype(int)
```

## Try 3: also add diabetes  <- go with this one

```{python}
m3 = smf.logit("sarcopenia ~ C(physical_activity, Treatment('Low')) + age + C(sex) + bmi + diabetes",
               data=cohort).fit(disp=False)
```

R’s glm() and Python’s statsmodels give the same results for the same model (OR 0.338, 0.796, etc.). Using both to cross-check during exploration is fine. The point is that the decided formula lives only in R’s models.R.

6.6.2 When some processing can only be done in Python

Some processing can only be done in Python (or is much easier there), such as MRI image analysis or machine learning. In that case, draw the language boundary at a file.

flowchart LR
  img["MRI images"] --> py["scripts/02_preprocess/<br/>segment.py (Python)"]
  py --> pq[("data/02_processed/<br/>muscle_area.parquet")]
  pq --> r["_targets.R (R)<br/>models, tables, figures"]

  • Python goes only as far as “writing the results to a file”
  • Everything after that (statistical models, tables, figures, numbers in the text) is read by R
  • Never write the same processing in both languages

From _targets.R, a file written by Python can be tracked with format = "file". If you change the Python side and write the file again, everything downstream on the R side is recomputed automatically.

6.6.3 Reading R data from Python

R’s .rds files cannot be read from Python. In the sample, a parquet file for Python is written alongside when the data are created.

scripts/01_fetch/make_dummy_data.R (end)
saveRDS(cohort, file.path(processedDataDir, "cohort.rds"))
arrow::write_parquet(cohort, file.path(processedDataDir, "cohort.parquet"))  # for Python (exploration)

6.7 Summary

  • Keep analysis/ results with freeze: auto, and note the commit at the top
  • Name notebooks starting with the date, and create new ones rather than overwriting
  • Keep materials for presentation separate in reports/
  • Finalized processing in R only. Python is for exploration, or goes “as far as writing a file”