R
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Data Structures
-
Section 4: Data Manipulation
-
Section 5: Visualization and Statistics
-
Section 6: Advanced R
-
Section 7: Practical Projects
-
Section 8: Interview Practice
-
Section 9: More Practice Exercises
-
Section 10: Shiny Apps in Depth
-
Section 11: More Data Wrangling
-
Section 12: Tidyverse Deep Dive
-
Section 13: Statistical Modeling Deep Dive
-
Section 14: Machine Learning in R
-
Section 15: R Visualization Deep Dive
-
Section 16: R Package Development Deep Dive
-
Section 17: R for Reproducible Research
-
Section 18: R and Databases
-
Section 19: R Performance Optimization
-
Section 20: Bioinformatics and Specialized R
-
Section 21: More Shiny Practice
-
Section 22: More Practice Exercises
-
Section 23: R Data Structures Deep Dive
-
Section 24: More Interview and Review
-
Section 25: R for Business Analytics
-
Section 26: R Text Mining and NLP
-
Section 27: R Spatial Data Analysis
-
Section 28: R Deep Learning
-
Section 29: Advanced Statistical Techniques
-
Section 30: R Object Systems Deep Dive
-
Section 31: R Environments and Metaprogramming
-
Section 32: R for Finance
-
Section 33: R for Clinical and Health Data
-
Section 34: More Shiny Advanced Practice
-
Section 35: R Data Cleaning Deep Dive
-
Section 36: R Reporting Automation
-
Section 37: More Practical Projects Round 2
-
Section 38: R Ecosystem and Career
-
Section 39: More Visualization Practice
-
Section 40: R for Experimentation
-
Section 41: R for Genomics and Bioinformatics Deep Dive
-
Section 42: R for Marketing Analytics
-
Section 43: R Data Import/Export Deep Dive
-
Section 44: R String Processing Deep Dive
-
Section 45: R for Actuarial and Insurance Analytics
-
Section 46: R Testing and Quality Assurance Deep Dive
-
Section 47: R Version Control and Collaboration
-
Section 48: R Advanced Functional Programming
-
Section 49: R for Supply Chain and Operations
-
Section 50: More Practice Exercises Round 3
-
Section 51: R Dashboards and BI Integration
-
Section 52: R Data Governance and Ethics
-
Section 53: More Modeling Practice
-
Section 54: R Final Capstone Projects
-
Section 55: R for Sports Analytics
-
Section 56: More Interview Practice Round 2
-
Section 57: R Networking and APIs
-
Section 58: R for Environmental Science
-
Section 59: R Notebook and Documentation Practices
-
Section 60: More Data Wrangling Mastery
-
Section 61: R for A/B Testing at Scale
-
Section 62: R Package Ecosystem Deep Dive
207: Reproducibility and Audit Trails
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
datain 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. Implementset.seed()with a value of your choice to ensure the test set is identical across every execution. - Step 3: Initialize the Environment. Use the
renvpackage to initialize a project environment. Runrenv::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.
There are no comments for now.