目标包中缺少文件时触发代码

Trigger code when a file is missing in targets package

考虑以下代码,我的 targets 管道中有两个文件。

options(crayon.enabled = FALSE, tidyverse.quiet = TRUE)
library(targets)
library(tidyverse)
write_csv(tibble(x1 = 1, x2 = 1), "a.csv")
write_csv(tibble(x1 = 1, x2 = 1), "b.csv")
tar_script({
  options(crayon.enabled = FALSE, tidyverse.quiet = TRUE)
  library(readr)
  list(
    tarchetypes::tar_files(paths, c("a.csv", "b.csv")),
    tar_target(data, read_csv(paths, col_types = "dd"), pattern = map(paths))
  )
})
tar_make()
#> ● run target paths_files
#> ● run branch paths_5c47d23d
#> ● run branch paths_63f6955e
#> ● run branch data_ecdaefee
#> ● run branch data_34ea7b1c
#> ● end pipeline

这对下游效果很有效,即如果我修改 b.csv,然后适当的分支下游 (data) 运行 再次。但是我如何创建一个向上游传播的目标(即如果 b.csv 丢失,那么我想要一个规则来 运行 重新创建它)。

谢谢,

在那种情况下,管道中的目标应该创建文件。素描:

# _targets.R file:
library(readr)
library(targets)
library(tibble)
options(crayon.enabled = FALSE, tidyverse.quiet = TRUE)

helper <- function(path) {
  write_csv(tibble(x1 = 1, x2 = 1), path) # 1. Write the file.
  path                                    # 2. Return the path.
}

list(
  tar_target(paths, c("a.csv", "b.csv")),
  tar_target(files, helper(paths), pattern = map(paths), format = "file"),
  tar_target(data, read_csv(paths, col_types = cols()), pattern = map(files))
)