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
235: Structuring a Reproducible R Project Layout
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:
- Directory Setup: Create a project root folder and inside it, create the following subdirectories:
data-raw,data-processed,R, andoutput. - 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/
- Code Refactoring: Open
main_analysis.Rand:- Remove the
setwd()call entirely. - Update the
source()call forhelper_math.Rto use the new relative path. - Update the
read.csv()call forraw_readings.csvto use the new relative path. - Update the
write.csv()orggsave()calls to point to thedata-processedoroutputfolders.
- Remove the
Confirm that the script runs from start to finish after you open the project using an .Rproj file located in the root directory.
There are no comments for now.