Onboarding to a defensive workflow

The minimum elements of a defensive workflow and links to learn more.

Mauro Lepore https://github.com/maurolepore

Table of Contents

This article overviews the minimum elements of a defensive workflow and points you to resources to learn more (video). Beyond onboarding to 2DII, this article should be useful if you suspect you’ve drifted into habits that slow you down or make your work more brittle (https://rstats.wtf/).

Setup RStudio to start from a blank slate every time you restart R

Use these settings:

Restart your R session often to confirm you can reproduce your results from a blank slate.

Use RStudio projects

RStudio makes it easy to create new projects2 and launch recent projects3. To launch older projects it’s easiest to use a dedicated app like Alfred4.

Practice safe paths

Find your project’s files with here().

Use it as a drop-in replacement for file.path(), it will always locate the files relative to your project root. – https://here.r-lib.org/reference/here.html


# Good
(path <- here("_posts", "2021-10-26-defensive-workflow", "data", "greeting.txt"))
#> [1] "/home/rstudio/2degreesinvesting.github.io/_posts/2021-10-26-defensive-workflow/data/greeting.txt"
#> [1] "Hello world"

# Bad
(path <- "/a/brittle path/that/only/i/have/data/greeting.txt")
#> [1] "/a/brittle path/that/only/i/have/data/greeting.txt"
#> Error in file(con, "r"): cannot open the connection

How to name files

Name your files following these guidelines:


  1. Shift + Ctrl/Cmd + P

  2. File > New Project

  3. File > Recent Projects


For attribution, please cite this work as

Lepore (2021, Oct. 26). Data science at 2DII: Onboarding to a defensive workflow. Retrieved from https://2degreesinvesting.github.io/posts/2021-10-26-defensive-workflow/

BibTeX citation

  author = {Lepore, Mauro},
  title = {Data science at 2DII: Onboarding to a defensive workflow},
  url = {https://2degreesinvesting.github.io/posts/2021-10-26-defensive-workflow/},
  year = {2021}