Setting up R projects

Theoretical and Empirical Research Methodology · Implementation Lab 3

Prof. Dr. Claudius Gräbner-Radkowitsch

Europa-University Flensburg, Department of Pluralist Economics

2 October 2026

Goals for today

  1. Learn how to set up an R project
  2. Learn about the difference between absolute and relative paths
  3. Learn how to use the here package

Part I: Why project management matters

Our goal

  • Learn a default directory structure
    • → Facilitates the collaboration with future-you considerably
  • A general workflow that avoids the classic project-management problems

Central idea: all results are reproducible from the raw data — and the raw data must never be manipulated

  • Raw data = what you download from the internet, gather through an experiment, or code yourself

How to keep your work transparent

Raw data must not be changed — but it is usually not in a state we can work with

  1. Gather data, save it on your computer
  2. Import this data into R
  3. Write a script that transforms raw data into tidy data
  4. Use the tidy data for further analysis: visualisation, modelling, …

Saving the scripts in steps 2 & 3 makes your work fully reproducible — they always tell you what you did to your raw data

Where we are heading

Set up your project environment · done only once per project

Import data → transform raw data into tidy data → save data · may be done several times

Where we are heading

Set up your project environment · done only once per project

Import data → transform raw data into tidy data → save data · may be done several times

Part II: Setting up your R project

Setting up your working environment

  • Where you save your data and files matters
  • Right directory structure as prerequisite for a transparent and reproducible project
  • For every task in R you should set up your project like this →
  • All relevant steps and the rationale described in Gräbner-Radkowitsch (2026)
  • ProjectName
    • ProjectName.Rproj
    • data
      • rawnever changed
      • tidyscript output
    • Ryour scripts
    • outputfigures, tables
    • misceverything else

Creating an R project

See the tutorial and the video for the documentation of the concrete steps

The slides focus on selected background concepts only

Part III: Paths and the here package

Absolute and relative paths

Two ways to tell your computer where a file is located:

  • Via an absolute path: the description starts at the root directory 🌲
  • Via a relative path: the description starts at your current position in the file system

The same file, two descriptions

Assuming we are located in DataScienceExercises and want to point to nycflights21_small.rda:

# absolute
"/Volumes/develop/teaching/DataScience/DataScienceExercises/data/nycflights21_small.rda"

# relative
"data/nycflights21_small.rda"
  • The relative path is shorter …
  • … and you can share code without forcing others to adjust the path

Relative paths and setwd()

  • Problem: how do we set our location to DataScienceExercises?
  • One option: give setwd() the absolute path:
setwd("/Volumes/develop/teaching/DataScience/DataScienceExercises")
  • Then we can use "data/nycflights21_small.rda"
  • Many people put setwd() at the top of their scripts

But you must never ever do this!

Why setwd() is evil

  • It does not avoid absolute paths — you still have to provide an absolute path to setwd() 🤯
  • It makes others hate you
    • Abby writes amazing_script.R
setwd("/Volumes/Macintosh HD/Users/AbbysUserName/PathToFolderThatOnlyExistsHere/ProjectName")
data_file <- data.table::fread("data/file.csv")
  • Sends it to Ellie who gets:
Error in setwd("/Volumes/Macintosh HD/Users/AbbysUserName/PathToFolderThatOnlyExistsHere/ProjectName") :
  cannot change working directory

The better alternative: here

  • A simple solution: the package here
  • It sets an anchor ⚓ in your project directory
  • You then create paths relative to that anchor via here::here()
    • Works on every machine
  • Always put here::i_am() into the first line of your scripts:
here::i_am("R/my_script.R")
library(here)
library(ggplot2)
# Script content
  • ProjectName
    • ProjectName.Rprojthe anchor ⚓
    • data
    • R
      • my_script.R
    • output
    • misc

Your turn

  • Create a new R project on your computer
  • Create all the required folders
  • Write an R script, put it into the right directory, and make it usable for the here package
  • Check out what here::here() returns and experiment with it
  • ProjectName
    • ProjectName.Rproj
    • data
      • raw
      • tidy
    • R
    • output
    • misc

Summary and outlook

  • We now know how to organise our working directory
  • Important difference between absolute and relative paths
  • The here package makes code transferable across machines — The better alternative to setwd()
  • ProjectName
    • ProjectName.Rproj
    • data
      • raw
      • tidy
    • R
    • output
    • misc

Project management is essential, and chronically under-appreciated

Further topics: version control (Git) · virtual environments (renv)

References

Gräbner-Radkowitsch, C. (2026) “Setting Up an R Project” C. Gräbner-Radkowitsch (ed.), Method and Inquiry, available at https://euf-methodology-hub.netlify.app/resources/setting-up-an-r-project.