-
Hello, Looking to selectively cancel targets based on a condition, eg. in this case if a certain column has the character 'A'. The library(targets)
summ <- function(dataset) {
tar_cancel('A' %in% dataset$z)
summarize(dataset, mean_x = mean(x))
}
# Set target-specific options such as packages.
tar_option_set(packages = 'dplyr')
# End this file with a list of target objects.
list(
tar_target(data, data.frame(x = sample.int(100), y = sample.int(100), z = LETTERS[1:5])),
tar_target(split, data %>% group_by(z) %>% tar_group(), iteration = 'group'),
tar_target(summary, summ(split), map(split))
) Output from tar_make() R: targets::tar_make()
• start target data
• built target data
• start target split
• built target split
• start branch summary_66865b4f
• cancel branch summary_66865b4f
• start branch summary_5d669193
• built branch summary_5d669193
• start branch summary_a2141425
• built branch summary_a2141425
• start branch summary_fbe2f44d
• built branch summary_fbe2f44d
• start branch summary_a7593665
• built branch summary_a7593665
• end pipeline Note we don't get the message "built pattern summary" and tar_read can't read the full summary object. It can however read individual branches. tar_read(summary)
Error: branches not in metadata: summary_388c2575
tar_read(summary, 3)
# A tibble: 1 x 1
mean_x
<dbl>
1 43.6 I'm trying to selectively cancel dynamic branches if they meet a certain criteria. Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
In your case, instead of
Then, the code in downstream dynamic targets could be written to handle
|
Beta Was this translation helpful? Give feedback.
In your case, instead of
tar_cancel()
, I recommend returning aNULL
value early.Then, the code in downstream dynamic targets could be written to handle
NULL
branches.tar_cancel()
makes no attempt to ensure that a target's value exists, and it makes no attempt to modify downstream dynamic branching behavior. I still like this limited scope because it is not likely to surprise users (surprises more often come from trying to do too much than too little) or create reproducibility problems far downstream. It also avoids a serious redesign on my end for the sake of a feature that would not …