-
Help
DescriptionI find myself using the following pattern a lot list(
tar_target(plot, plot_fn()),
tar_files(plot_files, save_fn(plot, "plot.png")
) The expression capturing in the source code of this package are beyond my understanding, so I was wondering whether there's an existing function available or how an implementation would look like such that I can instead use list(
tar_plot(plot, plot_fn, save_fn, path = "plot.png")
) Ideally it would track |
Beta Was this translation helpful? Give feedback.
Answered by
wlandau
Jan 19, 2023
Replies: 1 comment 2 replies
-
I will have to think about officially supporting this in # _targets.R file
library(targets)
library(tarchetypes)
tar_plot <- function(name, fun_plot, fun_save, path) {
name <- substitute(name)
prefix <- as.character(name)
name_plot <- paste0(name, "_plot")
name_file <- paste0(name, "_file")
list(
targets::tar_target_raw(
name = name_plot,
command = substitute(
fun(),
env = list(fun = substitute(fun_plot))
)
),
targets::tar_target_raw(
name = name_file,
command = substitute(
fun(plot, path),
env = list(
fun = substitute(fun_save),
plot = as.symbol(name_plot),
path = path
)
)
)
)
}
tar_plot(my_name, my_plot, my_save, "path.png") # R console
tar_manifest()
#> # A tibble: 2 × 2
#> name command
#> <chr> <chr>
#> 1 my_name_plot "my_plot()"
#> 2 my_name_file "my_save(my_name_plot, \"path.png\")" |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
Shians
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I will have to think about officially supporting this in
tarchetypes
. But for now, you can use your own target factory.