0

Consider this reprex:

targets::tar_dir({ # tar_dir() runs code from a temporary directory.
targets::tar_script({

  library(dplyr)

  values_df = tibble(index = 1:3, df = list(mtcars))

  list(
    tarchetypes::tar_map(
      values_df, names = "index",
      targets::tar_target(x, slice(df, index))
    )
  )
})
targets::tar_manifest()
})
#> # A tibble: 3 × 2
#>   name  command
#>   <chr> <chr>
#> 1 x_1   "slice(list(c(21, 21, 22.8, 21.4, 18.7, 18.1, 14.3, \n     24.4, 22.8, …
#> 2 x_2   "slice(list(c(21, 21, 22.8, 21.4, 18.7, 18.1, 14.3, \n     24.4, 22.8, …
#> 3 x_3   "slice(list(c(21, 21, 22.8, 21.4, 18.7, 18.1, 14.3, \n     24.4, 22.8, …

As you can see, values_df is a tibble with a numeric column "index" and a list column of data frames "df". However tar_map seems to drop the data frame class of the "df" elements, which will cause an error (because dplyr::slice() does not operate on lists). Am I doing something wrong? How do I prevent tar_map() from dropping the class of the elements of the "df" list column?

mikeck
  • 3,534
  • 1
  • 26
  • 39
  • Please see https://github.com/ropensci/tarchetypes/discussions/105 and try `df = quote(list(mtcars))` instead of `df = list(mtcars)`. – landau Mar 15 '23 at 01:35
  • @landau fair enough, that works for my trivial example. In reality my pipeline creates `values_df` from a function (reading a config file). I can make the creation of `values_df` a target and then use dynamic branching, but I was hoping to use static branching so that the resulting target names would be meaningful. – mikeck Mar 15 '23 at 15:12
  • This time you should use metaprogramming! This might be hard, but give [Hadley's book](https://adv-r.hadley.nz/expressions.html) a try. – Liang Zhang Jul 18 '23 at 15:51

0 Answers0