Skip to Content
Course content

207: Reproducibility and Audit Trails

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Imagine you're a lead chemist at a pharmaceutical company. You've just discovered a compound that lowers blood pressure with zero side effects. Your boss is thrilled, but there's a catch: to get FDA approval, you can't just say, "I mixed some chemicals, heated them up for a while, and it worked." You need a lab notebook that records the exact temperature, the precise brand of beaker used, the purity of the reagents, and the exact sequence of every stir and pour. If a scientist in a different lab in Tokyo follows your notes and gets a different result, your discovery is worthless. It's not a discovery; it's a fluke.

In R, reproducibility is your lab notebook. An audit trail is the evidence that your results aren't a fluke. When we talk about "reproducible research," we aren't just talking about whether the code runs without errors. We're talking about whether someone else can take your raw data and your script and arrive at the exact same decimal point in their final table.

The Nightmare of "It Worked on My Machine"

I've spent more hours than I care to admit debugging code that worked perfectly on a colleague's laptop but crashed on mine. Usually, it's because of absolute file paths. You'll see things like read.csv("C:/Users/Sarah/Documents/Project_Final/data.csv"). The moment Sarah sends that script to you, it breaks because you aren't Sarah, and you don't have her Documents folder.

The fix is simple but non-negotiable: use RStudio Projects (.Rproj files) and relative paths. When you work within a project, the root directory is the project folder itself. Your code becomes read.csv("data/data.csv"). Now, as long as the folder structure is maintained, the code runs anywhere—Linux, Mac, or Windows—without a single change.

Taming the Random Number Generator

R is great at randomness—sampling data, splitting sets for machine learning, or running simulations. But "random" in computing is actually "pseudo-random." It starts from a seed value. If you don't set that seed, R picks one based on the current time, meaning you'll get a different result every single time you run the script.

# This is dangerous for reproducibility
sample_data <- sample(my_dataset, 100) 

# This is a professional audit trail
set.seed(42) 
sample_data <- sample(my_dataset, 100)

By using set.seed(), you're essentially telling R, "Start the random sequence at this exact point." I usually pick a number I remember, like 42 or 123. It doesn't matter what the number is, as long as it's documented. If a reviewer asks why you chose those 100 patients for your study, you can prove it wasn't "cherry-picking"—it was a deterministic random sample.

Locking Down Your Dependencies

Here is a subtle trap: package versions. You might write a script using dplyr 1.0.0. A year later, a colleague runs it with dplyr 1.1.0. A function that used to return a data frame now returns a list, or a default argument has changed. Suddenly, your "reproducible" script produces different results or throws an error.

This is where renv comes in. Instead of just calling library(dplyr) and hoping for the best, renv creates a private library for your project. It records the exact version of every package you used in a renv.lock file. When someone else opens your project, they run renv::restore(), and R installs the exact versions you used. It's the digital equivalent of specifying the exact brand and purity of the reagents in that chemistry analogy.

The Script as the Single Source of Truth

The biggest enemy of an audit trail is the R Console. If you find yourself typing my_data$column <- as.numeric(my_data$column) directly into the console to "just fix one thing," you've broken your audit trail. You've performed a transformation that isn't recorded in your script.

I follow a strict rule: if it's not in the script, it didn't happen. If you make a discovery via trial-and-error in the console, copy that successful line back into your script immediately. Using Quarto or RMarkdown takes this a step further by interleaving your code, the output, and your reasoning in one document. That way, the "why" is preserved alongside the "how."




📋 Practical Task

Exercise: Building a Regulatory-Ready Blood Pressure Analysis

You have been handed a messy analysis of a blood pressure study. Your goal is to transform this "fragile" script into a reproducible audit trail. Complete the following steps:

  • Step 1: Eliminate Absolute Paths. Create a folder named data in your working directory and move a sample CSV file into it. Rewrite the data loading line to use a relative path.
  • Step 2: Stabilize the Sampling. The current script uses sample_n() to create a test set, but the results change every time it is run. Implement set.seed() with a value of your choice to ensure the test set is identical across every execution.
  • Step 3: Initialize the Environment. Use the renv package to initialize a project environment. Run renv::init() to capture the current state of your libraries and generate a lockfile.
  • Step 4: Document the Workflow. Move your final sequence of commands into a single R script (or RMarkdown file) that can be run from top to bottom without any manual intervention in the console.

Validation: Your exercise is successful if you can delete your .Rproj and environment, re-clone the project folder, run renv::restore() and your script, and arrive at the exact same mean blood pressure value as your first run.

Rating
0 0

There are no comments for now.

to be the first to leave a comment.