Onboarding to a defensive workflow

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

Mauro Lepore https://github.com/maurolepore
2021-10-26

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


library(here)

# 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"
readLines(path)
#> [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"
readLines(path)
#> Error in file(con, "r"): cannot open the connection

How to name files

Name your files following these guidelines:

https://speakerdeck.com/jennybc/how-to-name-files


  1. Shift + Ctrl/Cmd + P

  2. File > New Project

  3. File > Recent Projects

Citation

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

@misc{lepore2021onboarding,
  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}
}