Targets does not recognize other targets inside `values` of `tar_map()`

64 views Asked by At

I want to use another target to generate the values for tar_map(), this is to avoid end users to edit the code and just editing the config file. I tried this:

library(targets)
tar_script({
    library(tarchetypes)
    list(
         tar_target(config, c("a", "b", "c")), 
         tar_map(
           values = list(configs = config), 
           tar_target(result, paste0(configs, "1"))
          )
        )
})
tar_make()
#> Error:
#> ! Error running targets::tar_make()
#>   Error messages: targets::tar_meta(fields = error, complete_only = TRUE)
#>   Debugging guide: https://books.ropensci.org/targets/debugging.html
#>   How to ask for help: https://books.ropensci.org/targets/help.html
#>   Last error: object 'config' not found

Created on 2024-02-06 with reprex v2.1.0

Targets does not find the first target. If I use tar_read(config) works, but then the dependency is not recognized so if config is changed, the target is not updated.

1

There are 1 answers

2
landau On BEST ANSWER

The error happens because config is a target computed during the pipeline, whereas tar_map() is static branching that happens before the pipeline. You can use dynamic branching to branch over the config target: https://books.ropensci.org/targets/dynamic.html

library(targets)
library(tarchetypes)
list(
  tar_target(config, c("a", "b", "c")),
  tar_target(result, paste0(config, "1"), pattern = map(config))
)

The static branching version of this would define config as a global variable that exists before the pipeline begins.

library(targets)
library(tarchetypes)
config <- c("a", "b", "c")
list(
  tar_map(
    values = list(configs = config),
    tar_target(result, paste0(configs, "1"))
  )
)