Openscapes Champions Program
CC By Openscapes
Linked from: https://openscapes.org/series
Last updated 2025-05-20
1
Coding strategies for Future Us
Thinking for reuse
2
Source files: scripts & notebooks
3
Saving code is an absolute requirement for reproducibility. (Future you, future us)
Save commands as "scripts" (.R,.py) or “notebooks” (.Rmd, .qmd, .ipynb). It doesn't have to be polished. Just save it!
clicks
Name files deliberately
Jenny Bryan’s 3 rules for Naming Things (2015)
4
Jenny Bryan “Naming things” video (5 mins) from NormConf · Dec 4, 2022
Organize your work in projects
Report? R package? Chapter? Website? Whatever.
Can be the same unit as a GitHub Repo.
If using RStudio, it’s Project (capital P) .
all paths are relative to the project's folder.
Work on multiple projects at once w/ multiple instances of RStudio (or other software/IDE)
5
Project folder example: github.com/benmarwick/rrtools
Filepath Preamble
6
setwd("path/that/only/works/on/my/machine")
In R, we set the “working directory” file path using the command setwd()
Instead, be deliberate with file paths...
7
library(ggplot2)
setwd("/Users/jenny/cuddly_broccoli/verbose/foofy/data")
df <- read.csv(“raw_foofy_data.csv”)
p <- ggplot(df, aes(x, y)) + geom_point()
ggsave("/Users/jenny/cuddly_broccoi/ambiguous/fig.png")
Strategies for re-use: use relative file paths
If you’re using setwd() in your scripts, that’s ok, but be very disciplined:
i.e. an obvious & predictable place.
8
Strategies for re-use: Start R with a blank slate
Strategies for re-use: Avoid rm(list = ls())
In R, to start with a clean slate, it's common to see scripts begin with this object-nuking command: rm(list = ls())
10
Iterating & communicating with “literate programming”
Quarto, Jupyter notebooks combines code + text + output (tables, figures)
Analyses & figures in the same place as reporting document: saves time as you iterate!
Enables good practices for reproducibility & versioning
Simple text formatting
Learn: Quarto.org; jupyter.org
Code – R, Python, SQL, bash, others
Quarto
Quarto’s familiar outputs for science: Word documents and PDFs
Our Quarto file renders to:
Word!
Imagine never copy-pasting a graph into your report again!!!!
Quarto can also manage citations, cross- referencing figures and section headers.
PDF!
Quarto
Quarto creates HTML files that can be shared openly on the web
We can store and distribute html files on GitHub, combined as websites, slides, books
>> Reimagine sharing, sci comm, engagement, inclusion
Our Quarto file renders to:
HTML!
Suddenly you can share a URL rather than attaching a file!
And that same URL will update rather than re-attaching a new version of the file!
Further resources:
Workflows for (data) scientists with Python and R
14
15
16
Ideally, you don’t hardwire anything about your workflow into your product.
Workflow versus Product
Distinction between things you do because of personal taste & habits (“workflow”) versus the logic and output that is the essence of your project (“product”).
17
Workflow:
Clearly product:
From Bryan & Hester, RStudio, What they Forgot to Teach You About R
R Markdown
R Markdown powerfully combines executable R code with simple text formatting and for efficient, automatable, reproducible research
Analyses & figures in the same place as reporting document: saves time as you iterate!
Enables good practices for reproducibility & versioning
Simple text formatting
Learn: markdown.rstudio.com
Code – R, Python, SQL, bash, others
R Markdown
R Markdown’s familiar outputs for science: Word documents and PDFs
Our RMarkdown file renders to:
Word!
Imagine never copy-pasting a graph into your report again!!!!
R Markdown can also manage citations, cross- referencing figures and section headers.
PDF!
R Markdown
R Markdown creates HTML files that can be shared openly on the web
We can store and distribute html files on GitHub, which also offers display options for publishing.
>> Enabled the Ocean Health Index team to reimagine science communication & engagement.
Our RMarkdown file renders to:
HTML!
Suddenly you can share a URL rather than attaching a file!
And that same URL will update rather than re-attaching a new version of the file!
R Markdown
Transformed communication with OHI partners: methods, websites, tutorials
Example: Ocean Health Index
setwd(“~/github/bhi/baltic”)
tailorable frameworks (Lowndes et al. 2015);
Documentation: ohi-science.org/ohi-global-guide
23
R Markdown Visual Editor
Use the RStudio toolbar to add section headers, figures, tables, footnotes, citations, etc, à la Word, GDocs.
New Python capabilities, incl display of Python objects in the Environment pane
Learn more:
Introducing Visual R Markdown - J.J. Allaire’s blog
rstudio.github.io/visual-markdown-editing
25
By Allison Horst (tweet)
Software considerations for coding
26
Adapted from Tiffany Timbers, UBC Data Science, Intro to Reticulate, 2020
Type of tool needed:
Choosing the “best” tool for the job:
Opinionated analysis development (Parker 2017) peerj.com/preprints/3210