How to delegate tar_combine() into a function? #1571
-
Help
DescriptionI use tar_map() to create several targets and then combine them using tar_combine(). I want to create a column in the combined data frame containing a specified name (variable NAME below), not the target name. Hence, I created a little helper function However, this is not the most elegant solution. In the real code I'm going to have dozens of targets in the So, I wanted to move This does not work. It crashes with error It is trying to create a target called I tried forcing a new target name: but this creates an unevaluated target It seems to be a simple matter of encapsulating a bit of code in a function, but because of the way |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
|
It's usually much easier to supply the metadata in each branch and rely on defaults for library(targets)
library(tarchetypes)
sets <- tibble::tibble(
NAME = c("A", "B", "C"),
CYL = c(4, 6, 8)
)
mt_map <- tar_map(
values = sets,
names = NAME,
tar_plan(
mt = mtcars |> dplyr::filter(cyl == CYL),
md = mt |>
dplyr::summarise(mean = mean(disp)) |>
mutate(set = NAME) # Supply the name here
)
)
combined <- tar_combine(cm, mt_map[["mt"]]) # Use defaults for tar_combine().
list(mt_map, combined) |
Beta Was this translation helpful? Give feedback.
-
|
Thank you for your answer. Yes, I've been doing this too, though this requires repetition of |
Beta Was this translation helpful? Give feedback.
Hooks like
tarchetypes::tar_hook_outer()can add commands in bulk, andtarchetypes::tar_map()can condense even more repetition than that.It's possible, but the metaprogramming is usually not worth the trouble.