Reproducible Research

Course slides

Jean Golding Institute

Today’s mission

Make your research easier to re-run, understand and share.

How today works

Do it together. Then try it yourself.

  • Each topic is one short activity: we do a part together, you try something on your own, then I show the answer
  • The activities don’t need you to run any code: the only thing we run is Render
  • Everything you need is in the example folder
  • Stuck? Type in the chat

The journey

1 · Structure Where does everything go? Raw data, processed data, code and outputs, and never editing the raw data Hands-on: rescue a messy project

2 · Documentation How do I explain it, and report on it? A README, a data dictionary, and Quarto: one document, many formats Hands-on: be the new colleague, render a document

3 · Environments Will it run on another computer? Why environments matter, and how a lock file lets you send a project that works Hands-on: the new laptop test

4 · Your first step What will you do next? Pull it together into a checklist for your own project Hands-on: your checklist

By the end, you will be able to

  1. Organise a project into raw data, processed data, code and outputs, and explain why raw data is never edited
  2. Describe what a good README and data dictionary contain, and why they matter, particularly for Data Management Plans
  3. Explain why Quarto helps reproducibility, and make HTML, PDF and Word versions of a report from one document
  4. Explain why environments matter, and say what someone needs in order to re-run your project on another computer
  5. Apply these practices to your own coding projects, using the R and Python examples as a starting point
  6. Choose one practice to start using on your own project, and know where to find out more

01 Project structures

How should I organise a project so I, and others, can find things and re-run them?

Why bother?

Future you Code written as if others will use it is easier to return to after a holiday, or in six months’ time

Speed A project set up as a pipeline can be re-run quickly when the data changes or a reviewer asks for something new

Confidence You understand how the pieces fit together, and where things might go wrong

Sharing and careers Journals and funders increasingly expect shared code and data, and tidy work is easy to trust

Think about pipelines

Raw data → Clean → Analyse → Results

  • Never overwrite the raw data
  • Everything after it can be re-made by running the code
  • So the raw data and the code are what you must protect

Look at the messy project

Together 4 min

messy-project/
Copy of data.csv          notes.txt
analysis.R                plot1.png
analysis2 (copy).R        plot_new.png
analysis_OLD.R            report_draft.docx
clean.R                   results.xlsx
data.csv                  results_v3.xlsx
data_FINAL_USE_THIS.csv   untitled.R
data_final.csv
data_final_v2.csv

1

Which file is the raw data?

2

What do I run first?

3

Which figure came from which analysis?

Sort the messy project

Your turn 6 min

On your own, put every file under one heading:

Raw data Cleaned data Code Results Documents

Copy of data.csv          notes.txt
analysis.R                plot1.png
analysis2 (copy).R        plot_new.png
analysis_OLD.R            report_draft.docx
clean.R                   results.xlsx
data.csv                  results_v3.xlsx
data_FINAL_USE_THIS.csv   untitled.R
data_final.csv
data_final_v2.csv

Then decide: which file would you never edit, and which would you delete?

What goes where

Answer 4 min

Files What to do
Raw data data.csv Never edit it
Cleaned data data_final.csv, data_final_v2.csv, data_FINAL_USE_THIS.csv Three versions of one thing: make it from the raw data with code
Code clean.R, analysis.R, analysis2 (copy).R, analysis_OLD.R, untitled.R One script per step, numbered. Delete the copies
Results plot1.png, plot_new.png, results.xlsx, results_v3.xlsx The code can re-make these
Documents notes.txt, report_draft.docx Become the README and the report
Delete Copy of data.csv A duplicate

The tidy version

Answer 2 min

File or folder What it is
tidy-project/
├── README.md How to run it
├── report.qmd The report
├── _quarto.yml Report settings
├── data/
│ ├── raw/penguins.csv The original data: never edited
│ └── processed/
│ ├── penguins_clean.csv Made by the code
│ └── penguins_clean_dictionary.csv Explains each column
├── code/
│ ├── 01_clean_data.R Numbered, in the order to run them
│ └── run_all.R Rebuilds everything
└── outputs/ Made by the code: safe to delete

02 Documentation & Quarto

How do I explain my project, and report on it, so others (and future me) can use it?

Four kinds of documentation

Comments in code Files in the repo Data dictionary External documentation
Where Scripts and notebooks README.md A CSV next to the data A website or report
Why Explain why, not what How to use the project What each column means What you did, in plain terms
Who for Other researchers People running your code Anyone using the data Less technical readers
Example # outliers removed, as agreed with the lab Quickstart and setup bill_len: bill length, in mm A Quarto report


Write for the person who will read it, which includes yourself!

What goes in a README?

  • Summary and status
  • Quickstart: which files, in what order
  • Setup: what to install first
  • Contents: what each folder holds

Using AI

Be careful getting an LLM to create your README. While it can save time, you may inadvertently upload all your data and results by giving access to your folders! Instead, draft one yourself and then use a chatbot to help refine it!

✔ No code? A README is still the best way to document your project

Be the new colleague

Together 2 min

messy-project/notes.txt is the only documentation:

run clean.R first?? i think

then analysis2 (copy).R  NOT analysis.R (that one is old)

data_final_v2 has the weird rows removed BUT i fixed a few values by hand
remember to delete the weird rows first!!

figure 2 in the paper is plot_new.png (i think)
ask Sam about the Gentoo ones

You have inherited this project. What can’t you work out?

Be the new colleague

Your turn 4 min

Write down three things you need to know to work on this project.

Think about:

  • How do I run it, and in what order?
  • What software do I need?
  • What do the columns mean?
  • Which file is the real data?

The README

Answer 2 min

# Penguin body mass analysis

Body measurements of 333 penguins on three islands. **Status:** complete.

## Quickstart
1. Open this folder in RStudio or Positron
2. Run `code/run_all.R`

## Setup
- R 4.3 or later
- Quarto 1.4 or later

## Contents
- `data/raw/` the original data (never edited)
- `data/processed/` cleaned data, and a data dictionary
- `code/` the scripts
- `report.qmd` the report

The data dictionary

Answer 2 min

column type description units / values
species text Penguin species Adelie, Chinstrap, Gentoo
bill_len number Bill length mm
body_mass number Body mass grams
sex text Sex of the penguin female, male

What is Quarto?

  • Write text and code in one file
  • Render it into a report: HTML, PDF or Word
  • The results come from the code
  • Free and open source: quarto.org

report.qmd → HTML PDF Word

Why Quarto for reproducible research?

No copy and paste Numbers, tables and figures come straight from the code, so they can’t drift out of sync with the analysis

Always up to date If the data changes, render again and the whole report updates

One source, many outputs The same file can make HTML, PDF, Word, slides and websites

Plain text Easy to share, compare and keep in version control, in any editor

The old way vs the Quarto way

Without Quarto

  1. Run the analysis
  2. Copy numbers and plots into a Word document
  3. The data changes
  4. Repeat by hand, and hope you caught every number

With Quarto

  1. Write the report once, with the code in it
  2. Render
  3. The data changes
  4. Render again

Weaving text and analysis together

---
title: "Penguin report"
format: html
author: Josh Tyler
---

## Data

```{r}
clean <- read.csv("data/processed/penguins_clean.csv")
```

There are `{r} nrow(clean)` penguins in the data.

The four parts of a Quarto file

1 · Header

The title and the output format

2 · Text

Ordinary Markdown, with headings and lists

3 · Code chunk

The code runs when you render, and its output appears underneath

4 · Inline code

A result inside a sentence, recalculated every time

If the data changes, the report updates

New data → Re-run the code & Render → Updated report

  • Every number, table and figure is recalculated
  • Sentences with inline code update too
  • There is nothing to copy and paste

✔ Change the data (or the code), not the report

Live demo: change the data

Live demo 4 min

  1. Open the report, which is made from the data
  2. Change the data
  3. Render: every number changes

One document, many formats

Together 6 min

report.qmd → HTML PDF Word

One file. Three formats.

The PDF is made with Typst, so you don’t need LaTeX.

One document, many formats

Your turn 7 min

  1. Open one-document/report.qmd
  2. Change the title, the author and one sentence
  3. Render it as HTML, then PDF, then Word
  4. Open all three: did your changes appear in each?

Press Render (choose the format), or run quarto render report.qmd to make all three. Not working? Open the files in finished/.

What else can Quarto make?

Answer 3 min

Reports HTML, PDF and Word, with numbered figures, tables and equations that you can cross-reference, and citations

Slides This deck is a Quarto presentation

Websites This course website is built with Quarto

Papers and books Journal articles, theses and books, built from several files

Learn more about Quarto

03 Virtual environments

How do I make sure my code still runs next month, or on someone else’s computer?

The problem: “it worked last year”

  1. Last year: your analysis runs, using version 1.2 of a package
  2. This year: a new project needs version 1.3, so you update
  3. You re-run last year’s analysis: it breaks, or gives different results 💥

Your computer has one shared set of packages, so two projects that need different versions can’t both be happy

Why does this happen?

Packages change Functions are renamed, defaults change and bugs are fixed

Your code depends on them The same code with a different package version can give a different result

Other computers differ A colleague, a new laptop or an HPC: “it works on my machine”

Nothing records it Your project doesn’t say which versions it needs, so nobody can tell what went wrong

What is an environment?

Without environments

One shared library of packages for every project on your computer

With environments

Each project has its own set of packages, at the versions it needs

Project 1 → package v1.2

Project 2 → package v1.3

How do environments work?

Lock file → Create the environment → Packages installed

  • The lock file is the recipe: an exact list of packages and versions. It is small, and you keep it with your project
  • The environment is what gets built from it: the installed packages, in their own folder
  • Your code only sees the packages in its own environment
  • The environment is disposable: delete it, and rebuild it from the lock file whenever you like

The tools

Python R
Tool uv renv
Lock file (keep it) uv.lock renv.lock
Environment (disposable) .venv renv/library
Rebuild it uv sync renv::restore()

Send your project to someone, and it works

Your project +
lock file
→ Colleague rebuilds
the environment
→ Same packages,
same results

✔ Send: your code, README and lock file (and the data, if you can)

✘ Don’t send: the environment itself. It is large, and specific to your computer

What environments don’t cover

  • They record packages. Also write down the version of R or Python in your README (uv can pin Python; renv records the R version but doesn’t install it)
  • The operating system and hardware can still differ
  • They don’t include your data

Good enough for almost every project. Perfect is not the goal!

No code? Record it anyway

Write a list of programs

Record the versions

List your plugins

Note your operating system

All of this can live in your README: the next person then knows what you used!

Live demo: delete it, then rebuild it

Live demo 5 min

  1. Here is a project’s environment
  2. Delete it
  3. Rebuild it from the lock file: one command

The new laptop test

Together 2 min

A colleague emails you their project: “It runs fine on my laptop!”

You open it on your laptop:

Error: there is no package called 'ggplot2'

What would you need to know to make it run?

The new laptop test

Your turn 6 min

On your own, list everything you would need to know to run this project on a new computer.

Write down as many things as you can. There is no wrong answer.

The new laptop test

Answer 3 min

You need to know Where it should be written down
Which software, and which version The README (Setup)
Which packages, and which versions A lock file
Where the data is The README, and a data/raw folder
What to run, and in what order The README (Quickstart), or one script
Where the results come from The code, not copy and paste

Learn more about environments

04 Your first step

What will you do differently on Monday?

From messy to shareable

Together 3 min

Messy → Tidy → Documented →

Reported → Recorded → Shareable

  • Tidy: raw data kept apart and never edited
  • Documented: a README and a data dictionary
  • Reported: one document, made from the data and code
  • Recorded: the software and versions written down
  • Shareable: a project a colleague can run on a new computer

Your checklist

Your turn 6 min

Tick what you do. Circle one to start. Post it in the chat.

Structure

☐ Raw data never edited

☐ Separate folders

☐ Clear file names

Documentation

☐ A README

☐ A data dictionary

☐ A report made from code

Environments

☐ Software versions written down

☐ Packages recorded in a file

☐ A colleague could run it

Sharing

☐ Data in an open format (CSV)

☐ A licence

☐ Somewhere to deposit it

Quick wins

Answer 3 min


Quick win Time Why it helps
Stop editing the raw data 0 min Everything can be re-made
Clear folders and file names 15 min You can find things
Write a README 15 min Others, and future you, can run it
Write down your software versions 5 min It runs on another computer
Describe your columns 20 min Your data makes sense to others

What you can now do

  • ✔ Organise a project, and never edit the raw data
  • ✔ Describe what a good README and data dictionary contain
  • ✔ Make HTML, PDF and Word versions of a report from one document
  • ✔ Say what someone needs to re-run your project on another computer
  • ✔ Apply these practices to your own coding projects, in R or Python
  • ✔ Choose one practice to start using this week

A project that does all of this is already most of the way to reproducible

Where next?

  • The Turing Way
  • Bristol Training courses
  • Version control: see our git and GitHub course
  • Sharing data (Q-FAIR): the next few slides, and the notes
  • Try it all yourself: the capstone exercise (linked from the course home page) builds a whole project, with an environment and a report

Thank you

Questions?

★ Optional: if there is time

Sharing your data: Q-FAIR

Q-FAIR data principles

Quality Accurate, complete, documented

Findable Clear names and description

Accessible Clear how to get it

Interoperable Open, standard formats

Reusable Documented, with a licence

Read more: the FAIR principles

Why it matters

  • Funders and journals increasingly expect data to be shared
  • Others can build on your work, and check it
  • Future you can find and understand your own data
  • Your work gets cited

Four things to do

What to do Learn more
Open formats Share data as CSV, not a format that needs one program Turing Way: research data management
Describe it Write a data dictionary: what each column means, its units and missing values See section 2
Add a licence Data: CC BY 4.0. Code: MIT Creative Commons chooser, choosealicense.com
Deposit it A repository that gives a DOI, such as Zenodo or your institution’s Zenodo

Can’t share the data? Share the README and data dictionary, and explain how to ask for access

Q-FAIR detective

Your turn 4 min

A A spreadsheet called final_data_v7.xlsx on my laptop. Email me if you want it. I would have to remember what some of the columns mean.

B A CSV in a repository, with a DOI, a README, a data dictionary and a CC BY licence.

C A CSV on the department shared drive, with a README. No licence, and you need to ask the group’s admin for access.

For each one, mark every letter of Q-FAIR with a ✔ or an ✘.

Q-FAIR detective

Answer 2 min

Q F A I R
A The spreadsheet ✘ ✘ ✘ ✘ ✘
B The repository CSV ✔ ✔ ✔ ✔ ✔
C The shared-drive CSV ✔ ✘ partly ✔ ✘

C is documented (Q) and in an open format (I), but hard to find (F), needs permission (A), and has no licence (R).