0

考虑以下代码,我的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缺少,那么我希望运行一个规则来重新创建它)。

谢谢,

4

1 回答 1

1

在这种情况下,管道中的目标应该创建文件。草图:

# _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))
)
于 2021-03-05T12:47:45.483 回答