Building reproducible workflows with {targets}
Overview
Have you ever created files named “01_clean.R”, “02_analysis.R”, and “03_plots.R” and forgotten to run one of the files in the right order? Have you had to manually rerun an entire analysis after a single input changes—even if that input only affected one plot? Data analysis involves many moving parts. Traditional script-based approaches often lead to fragile pipelines, wasted computation, and reproducibility nightmares. Fortunately, the {targets} package for R offers a powerful solution by implementing a Make-like pipeline toolkit that transforms chaotic scripts into streamlined, automated workflows that intelligently skip unnecessary steps.
Materials
All the materials we’ll use today are three self-contained RStudio projects:
Packages
You’ll need all these packages on your computer if you’re following along:
install.packages(
c("tidyverse", "ggrepel", "sf", "tigris", "dataverse", "jsonlite",
"targets", "tarchetypes", "tidybayes", "brms",
"marginaleffects", "modelsummary", "parameters", "patchwork"
)
)Slides
Other examples and resources
Official documentation
- {targets} book
- {targets} site
- {tarchetypes} site (extra functions for rendering to Quarto or R Markdown, using a URL as a storage format, conditionally controlling target execution, etc.)
Pipelines for research projects
“Navigating Hostility: The Effect of Nonprofit Transparency and Accountability on Donor Preferences in the Face of Shrinking Civic Space”
Analysis notebook + preprint and_targets.R“Derogations, Democratic Backsliding, and International Human Rights During the COVID-19 Pandemic”
Analysis notebook + preprint and_targets.R“Pandemic Pass? Treaty Derogations and Human Rights Practices During COVID-19”
Analysis notebook + preprint and_targets.R
Pipelines for course websites
Data Visualization with R
(pipeline builds data, zips project folders, converts HTML slides to PDF, builds Quarto website, and uploads everything to a server)
Website and_targets.RProgram Evaluation for Public Service
(pipeline builds data, zips project folders, converts HTML slides to PDF, builds Quarto website, and uploads everything to a server)
Website and_targets.RGovernance Around the World
(pipeline builds Quarto website and uploads everything to a server)
Website and_targets.R
Pipelines for Idaho’s 2024 election
- DIY Election Desk (example repository with two independent pipelines: one for processing data and one for building a site)
- “Election Night Reporting Using R & Quarto” posit::conf(2025) talk