{targets}
https://papsti.github.io/talks/2025-01-22_targets.html
library(readr); library(dplyr); library(ggplot2)
library(doParallel); cl <- makeCluster(4); registerDoParallel(cl)
# read data
data <- read_data("data/case-counts.csv")
# specify parameters
foreach(scenario = c("A", "B", "C")) %dopar% {
# simulate
sim <- make_forecast(
data,
scenario = scenario
)
# plot results
plot_forecast(sim)
}
source("forecast.R")
Other management code:
for(scenario in c("A", "B", "C")){...}
saveRDS(sim)
{targets}
by writing a pipeline.A pipeline is a layer on top of your analysis code that encodes the recipe for a sequence of generated objects, called “targets”, and how these targets depend on each other.
Warning message:
package 'targets' was built under R version 4.3.3
{targets}
is an R package that you can use to write, manage, and execute pipelines.
_targets.R
filelibrary(targets)
source("R/make_forecast.R"); source("R/plot_forecast.R")
tar_option_set(packages = c("readr", "dplyr", "ggplot2", "lubridate"))
# pipeline
list(
# read data
tar_target(
data,
read_data("data/case-counts.csv")
),
# simulate
tar_target(
sim,
make_forecast(data)
),
# plot results
tar_target(
plot,
plot_forecast(sim)
)
)
▶ dispatched target data
● completed target data [2.01 seconds, 357 bytes]
▶ dispatched target sim
● completed target sim [0.01 seconds, 661 bytes]
▶ dispatched target plot
● completed target plot [0.01 seconds, 95.58 kilobytes]
▶ ended pipeline [2.33 seconds]
Warning messages:
1: package 'targets' was built under R version 4.3.3
2: package 'readr' was built under R version 4.3.3
3: package 'ggplot2' was built under R version 4.3.3
4: package 'lubridate' was built under R version 4.3.3
5: 1 targets produced warnings. Run targets::tar_meta(fields = warnings, complete_only = TRUE) for the messages.
Warning message:
package 'targets' was built under R version 4.3.3
Before
✔ skipping targets (1 so far)...
▶ dispatched target plot
● completed target plot [0.05 seconds, 95.671 kilobytes]
▶ ended pipeline [4.04 seconds]
Warning messages:
1: package 'targets' was built under R version 4.3.3
2: package 'readr' was built under R version 4.3.3
3: package 'ggplot2' was built under R version 4.3.3
4: package 'lubridate' was built under R version 4.3.3
Distributed computing: parallelize easily with crew
Branching: easily repeat sections of the pipeline1
setup
{targets}
tutorial{targets}
walkthrough{targets}
tutorial{targets}
supports {tidyselect}
syntax!Functions allow you to automate common tasks in a more powerful and general way than copy-and-pasting. Writing a function has four big advantages over using copy-and-paste:
- You can give a function an evocative name that makes your code easier to understand.
- As requirements change, you only need to update code in one place, instead of many.
- You eliminate the chance of making incidental mistakes when you copy and paste (i.e. updating a variable name in one place, but not in another).
- It makes it easier to reuse work from project-to-project, increasing your productivity over time.
✔ skip target data
▶ start target sim
✖ error target sim
▶ end pipeline [0.278 seconds]
Error:
! Error running targets::tar_make()
Error messages: targets::tar_meta(fields = error, complete_only = TRUE)
Debugging guide: https://books.ropensci.org/targets/debugging.html
How to ask for help: https://books.ropensci.org/targets/help.html
Last error: object 'x' not found
Don’t do this!
Do this
dev
for .R
filesnotes
for .Rmd
filestar_load()
and tar_read()
in development{targets}
makes this move easier for analysis projects{targets}
{targets}
pipeline?”{targets}
pipelines (and R packages)
R/
{roxygen2}
(once they’re stable)https://papsti.github.io/talks/2025-01-22_targets.html