rtargets-r-package

Trigger code when a file is missing in targets package


Consider the following code, where I have two files in my targets pipeline.

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

This works well for downstream effects i.e. if I modify b.csv, then the appropriate branches downstream (data) run again. But how would I create a target that propigates upstream (i.e. if b.csv is missing, then I want a rule to run to recreate it).

Thanks,


Solution

  • In that case, targets in the pipeline should create the files. Sketch:

    # _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))
    )