ATAP Workshop: Folder Organisation

Introduction

Good folder organisation is one of the simplest but most important aspects of reproducible research. Structuring projects clearly makes it much easier to:

  • navigate analyses
  • locate outputs
  • avoid overwriting files
  • collaborate with others
  • reproduce workflows later

Throughout this workshop we assume that analyses are being run inside an R Project.

Start by creating a folder that can house all of this work along the lines of ATAP workshop. Open R and save a new project in this folder.

Shooting from the hip in the beginning will come back to bite you as projects grow in complexity!

Check your R version

Before starting, it is useful to check the version of R that you are using.

Code
R.Version()

Installing and Loading Packages

We will use the pacman package as a convenient way to install and load packages simultaneously. You can still use install.packages() and library, but pacmam does the heavy lifting if you don’t have certain packages installed already.

Code
if (!requireNamespace("pacman", quietly = TRUE)) {
  install.packages("pacman")
}

Load the required package:

Code
pacman::p_load(purrr)

Working Directories

A working directory is the folder where R will look for files and save outputs by default.

Check your current working directory:

Code
getwd()

Because we are working inside an R Project, the working directory is automatically set to the project root when the project is opened. So you can simple navigate all your folders once created with “data/detections.csv” for example

Hardcoding wd with setwd() limits portability of a project and becomes cumbersome when projects grow.

Creating Project Folders

A clean project structure helps separate: - raw data - scripts - plots - tables - manuscript files

We first create a path object pointing to the project location.

Code
folder_path <- "."

Because we are working inside an R Project, "." refers to the project root directory. Instead of hardcoding

Define Folder Names

Choose the folders you want to create. Common examples include: - data - scripts - plots - tables - paper

Remember: - folder names are character strings and must be enclosed in quotation marks - avoid accidental spaces in folder names

Code
folders <- c(
  "data",
  "scripts",
  "plots",
  "tables",
  "paper"
)

Create the Folders

We can use purrr::walk() to iteratively create all folders in one call.

Code
purrr::walk(
  file.path(folder_path, folders),
  dir.create,
  recursive = TRUE,
  showWarnings = FALSE
)

Check the Folder Structure

Finally, confirm that the folders were created successfully.

Code
list.dirs(folder_path, recursive = FALSE)
 [1] "./.git"                        "./.quarto"                    
 [3] "./.Rproj.user"                 "./3_exploratory_figures_files"
 [5] "./data"                        "./docs"                       
 [7] "./paper"                       "./plots"                      
 [9] "./scripts"                     "./site_libs"                  
[11] "./tables"                     

Easy and now we can get stuck into it with peace of mind that things are easy to find.