Organizing a project

Defensive R Programming

Dr. Colin Gillespie

Jumping Rivers

It starts with something small

All R analyses start with a little code, but then

  • 1 line becomes 10
  • 1 imported package becomes 5
  • 1 file becomes a mess
Defensive R Programming

Project Set-up

Every project I work on

  • Has its own directory
  • Has a sensible name

The directory name gives the context of the scripts

Defensive R Programming

Directory: input/

This directory contains data, typically

  • csv & excel files
  • No R code

Data is only edited in R

Defensive R Programming

Directory: R/

All R code lives in this directory

Notice The directory isn't

  • R_analysis
  • R_code
  • R_survival

just plain R/

Defensive R Programming

Directory: R/

In this directory, I always have a file called

  • load.R

This file loads the data from input/

Every project I've worked has a similar structure

  • I can give you any project and you can load the data

Defensive R Programming

The load.R file

All paths are relative

battles <- read_csv("input/battles.csv")
foes <- read_xlsx("input/foes.xlsx")

My code is portable

Defensive R Programming

Other R files

Remember, all R files live in the R directory!

  • clean.R - for cleaning your data
  • function.R - any helper functions
  • analysis.R - the actual analysis

Standard names used in every project

Defensive R Programming

Your turn

Defensive R Programming

Preparing Video For Download...