Files, Folders, and Project Discipline

Steen Flammild Harsted & Søren O’Neill

R, the Tidyverse, and Basic Data Science Principles

R and the Tidyverse

Basic Data Science Principles


The Data Sheriff:

  • Cares about Files, Folders, and Project Discipline
  • Lurks in the margins of some exercises

File and Folder names





Your code might be passed between programs.
Good file and folder names will secure safe transfer



image from https://quarto.org/docs/faq/rmarkdown.html

File and Folder names

Good file and folder names:

  • short
  • informative
  • have words separated by _
  • have dates in the format YYYY_MM_DD

post_hoc_analysis.qmd
2024_08_01_project_data.csv
01_import.R
raw_data
clean_data

Rstudio Projects

  • Better file organization
  • Easier navigation
  • Enhanced reproducibility

Creating an R Project

  1. Open RStudio
  2. Click on File -> New Project
  3. Select New Directory -> New Project
  4. Name your project and choose a location
  5. Click Create Project

Suggested Project Structure

MyProject/
├── clean_data/
├── plots/
├── raw_data/
├── scripts/
├── tables/
└── MyProject.Rproj

Using the here Package

Overview

  • Purpose: Simplifies file path management in R projects.
  • Problem: Absolute paths are not portable and can break when moving projects between computers.
  • Solution: here constructs paths relative to the project root.

Benefits

  • Portability: Paths work regardless of the user’s working directory
  • Flexibility: Paths work regardless of the user’s operating system
  • Clarity: Improves readability and maintainability of code.
  • Integration: Works seamlessly with RStudio projects.

here()

  • here() starts at the project root and constructs the path to the specified file.
  • List the directories and file names as separate arguments.
library(here)
here()
[1] "/home/soon/Research/proj_aktive/r4phd.sdu.dk"


::: {.cell}

here("raw_data")
[1] "/home/soon/Research/proj_aktive/r4phd.sdu.dk/raw_data"

:::
::: {.cell}

here("raw_data", "soldiers.csv")
[1] "/home/soon/Research/proj_aktive/r4phd.sdu.dk/raw_data/soldiers.csv"

:::