An introduction to targets for R
R-Ladies Santa Barbara
October 11, 2023
Tracey Mangin
1
we have 15 slides!
Agenda
Resources
2
In a perfect world…
3
import inputs
clean and process data
perform analysis
summarize/visualize outputs
we’re done hooray!
In a perfect world…
Reality…
4
import inputs
clean and process data
perform analysis
summarize/visualize outputs
import inputs
clean and process data
perform analysis
summarize/visualize outputs
update inputs
change cleaning
change this a thousand times and add a million more steps
are these right?
next time, i promise we’ll be perfect…
we’re done hooray!
Workflow challenges
5
Enter targets package for R
6
target explained
7
function
clean_data <- function(file) {
data <- read_csv(file) %>%
filter(!is.na(date_time)) %>%
mutate(day = weekdays(date_time)
data
}
target
tar_target(name = data, command = clean_data(file))
other examples
tar_target(name = max_val, command = 16))
tar_target(name = save_data, command = simple_write(data.csv), format “file”)
R code to run target
target name
targets setup
8
Inspect pipeline once targets filled in
9
functions
tar_manifest()helps check for obvious errors and produces a data frame of info about the targets in the pipeline
tar_visnetwork()visualizes pipeline workflow
tar_glimpse()visualizes pipeline workflow faster than visnetwork, but doesn’t account for progress info
tar_visnetwork()
tar_glimpse()
Run the pipeline
10
Making changes and rerunning
11
tar_visnetwork()
Debugging
12
Debugging steps
13
More advanced info and resources
CHECK THESE OUT!
14
Demo
15