Skip to Content
Course content

235: Structuring a Reproducible R Project Layout

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

I was digging through an old analysis I did for a city planning project a few months ago—something tracking urban air quality sensors—and I hit a wall immediately. I opened my main script, hit "Run All," and was greeted by a wall of red text. The first error? Error in setwd("C:/Users/jdoe/Desktop/AirQuality_Project_v2") : cannot change working directory.

The "It Works on My Machine" Trap

I remember exactly why I wrote that line. At the time, I wanted the script to know exactly where the data was. But I've since moved the project to a different drive, and my username isn't "jdoe" on my laptop. I've fallen into the classic trap of hard-coding absolute paths.

Let's try to fix this. If I just comment out the setwd() line and try to read the data using read.csv("sensor_data.csv"), R looks in the current working directory. If I'm lucky and my RStudio session is already pointed at the folder, it works. But if I open a different script first, or launch R from the terminal, it fails again. It's brittle. I'm essentially telling R, "Assume you are standing in this exact spot," without giving R a way to find that spot reliably.

Fighting the File Clutter

Once I got the path working, I looked at my project folder. It was a nightmare. I had analysis_v1.R, analysis_v2_final.R, analysis_v2_final_FIXED.R, and about five different CSV files scattered in the root directory. It's hard to tell which data is the raw "source of truth" and which is a cleaned-up version I created halfway through the analysis.

I tried grouping them by just creating a folder called "Stuff" and dumping everything in there. But then my scripts had to look like read.csv("Stuff/sensor_data.csv"). It's slightly better, but "Stuff" isn't a professional convention. If a collaborator joined this project, they'd have no idea where to start.

Building a Predictable Skeleton

I decided to stop winging it and implement a structure that actually scales. I started by creating a few specific directories. I didn't want to over-engineer it, but I needed boundaries.

# I created these manually in my file explorer:
# /data-raw    <- The untouched original CSVs
# /data-clean  <- Processed data ready for analysis
# /R            <- Custom functions and helper scripts
# /output      <- Plots, tables, and exported reports
# /docs        <- Notes and methodology

Now, here is the "aha!" moment. Instead of using setwd(), I created an R Project file (.Rproj) in the root directory. When I open the project via that file, RStudio automatically sets the working directory to the root of that folder. Now, my paths are relative to the project root, regardless of where the project lives on the hard drive.

Let's see how the code looks now:

# No setwd() here! 
# The .Rproj file handles the location.

# Load a helper function from my R folder
source("R/cleaning_functions.R")

# Read raw data from the raw folder
raw_data <- read.csv("data-raw/air_quality_sensors.csv")

# Process it and save it to the clean folder
clean_data <- clean_sensor_logs(raw_data)
write.csv(clean_data, "data-clean/air_quality_cleaned.csv")

# Save the final plot to output
ggsave("output/pollution_trend_plot.png")

This feels significantly more stable. If I zip this entire folder and send it to you, you can open the .Rproj file, and every single path will resolve perfectly on your machine without you changing a single line of code. That is the essence of reproducibility: the environment travels with the code.




📋 Practical Task

Reorganizing the Urban Air Quality Analysis Folder

You have been handed a messy project folder for an air quality study. The current state is a single directory containing: main_analysis.R, helper_math.R, raw_readings.csv, cleaned_readings.csv, final_graph.png, and notes.txt. The main_analysis.R script currently starts with setwd("C:/Users/Student/Documents/AirQuality").

Your task is to restructure this project for reproducibility. Perform the following steps:

  1. Directory Setup: Create a project root folder and inside it, create the following subdirectories: data-raw, data-processed, R, and output.
  2. File Migration: Move the files into their appropriate folders:
    • raw_readings.csv $\rightarrow$ data-raw/
    • cleaned_readings.csv $\rightarrow$ data-processed/
    • helper_math.R $\rightarrow$ R/
    • final_graph.png $\rightarrow$ output/
  3. Code Refactoring: Open main_analysis.R and:
    • Remove the setwd() call entirely.
    • Update the source() call for helper_math.R to use the new relative path.
    • Update the read.csv() call for raw_readings.csv to use the new relative path.
    • Update the write.csv() or ggsave() calls to point to the data-processed or output folders.

Confirm that the script runs from start to finish after you open the project using an .Rproj file located in the root directory.

Rating
0 0

There are no comments for now.

to be the first to leave a comment.