Setting Up an R Project
Organize your files so your analysis is reproducible and easy to share
Use RStudio Projects (.Rproj)
An RStudio Project keeps everything for one analysis — scripts, data, and results — together in a single folder. Instead of scattering files across your desktop and downloads, you work inside one self-contained folder that you can copy, back up, or hand to a colleague as a unit.
To create one in RStudio, go to File → New Project → New Directory → New Project, choose a name and a location, and click Create Project. RStudio makes the folder and puts a small .Rproj file inside it (for example my_project.Rproj).
That .Rproj file is what makes projects worthwhile:
- Open the project by double-clicking the
.Rprojfile. RStudio opens with that folder as the working directory — the place R looks for your files. - Because the working directory is set automatically, you never need
setwd()with a hard-coded path. This is the single biggest step toward reproducible code. - Everything stays relative to the project folder, so the project works the same on any computer.
Start every new practical by creating a project first, then writing code. A few seconds of setup saves a lot of “R can’t find my file” confusion later.
A recommended folder structure
Inside your project folder, group files by what they are. A simple, reliable layout looks like this:
📁 my_project/
├── 📄 my_project.Rproj # the project file — open this to start
├── 📁 data/ # raw data files (read-only)
├── 📁 R/ # your .R analysis scripts (some prefer scripts/)
├── 📁 output/ # generated results, cleaned data
└── 📁 figures/ # saved plots
📁 = folder · 📄 = file
| Folder / file | What goes here |
|---|---|
data/ |
Original data exactly as you received it. Treat it as read-only — never overwrite the raw files. |
R/ (or scripts/) |
Your .R scripts that import, clean, analyse, and plot. |
output/ |
Results your code produces: cleaned datasets, tables, summaries. |
figures/ |
Plots you save with ggsave() or similar. |
Keeping raw data separate and untouched means you can always re-run your analysis from scratch and get the same result. If something goes wrong, your original data is still safe.
Every dataset on this site lives in a data/ folder and is read with a project-relative path such as read_csv("data/tablet_weights.csv"). When you set up your own project the same way, the examples here work unchanged.
Grab this exact layout as a working project: Download the example project (ZIP).
Unzip it, open my_project.Rproj in RStudio, then open R/analysis.R and click Source to run it. Using the tablet_weights.csv dataset, it prints a per-batch summary, writes output/batch_summary.csv, and saves figures/weight_by_batch.png — no setup needed beyond having the tidyverse installed.
Paths and the working directory
Once you work inside a project, always refer to files with project-relative paths — paths that start from the project folder, not from the top of your hard drive.
# Do this — relative to the project folder
tablet_weights <- read_csv("data/tablet_weights.csv")# Avoid this — an absolute path tied to one computer
tablet_weights <- read_csv("C:/Users/Morten/Documents/practical3/data/tablet_weights.csv")
# Avoid this — changing the working directory by hand
setwd("C:/Users/Morten/Documents/practical3")A path like C:/Users/Morten/... only exists on your machine. The moment a classmate, a teacher, or a lab computer opens your script, it breaks. Project-relative paths work everywhere, so your analysis stays reproducible.
File naming conventions
Consistent file names make a project easy to scan and keep scripts in a sensible order.
- Use
snake_case: lowercase words joined by underscores. - No spaces and no special characters — they cause problems on different systems.
- Number scripts when they run in sequence, so they sort in run order:
R/
├── 01_import.R # read the raw data
├── 02_clean.R # tidy and compute new columns
├── 03_analysis.R # statistics and models
└── 04_figures.R # produce plots
These rules are for file and folder names. For naming variables, functions, and columns inside your R code, follow the R Coding Conventions — the same snake_case idea, with units in the name (fill_mass_mg, pct_label).
Reproducibility habits
A few small habits make your work reliable enough that anyone — including you, months later — can reproduce it:
- Load packages at the top of every script with
library(), so it is clear what the script needs. - Restart R with a clean environment and re-run the whole script from the top before you trust a result. This proves the script works on its own, not because of leftover objects in memory. (In RStudio: Session → Restart R.)
- Keep raw data read-only — save cleaned data to
output/instead of overwriting files indata/.