Passing dots arguments through a target factory #84
-
I am trying to build a target factory which is a combination of a file target and a target running a read function - it should take a path and a function as arguments, and return two targets: a file target and a the result of reading e.g. tar_read_data(data, "data.csv", read_csv, skip = 5) where More generally, something along these lines might be a good addition in tarchetypes? I feel I use the pattern of these two targets quite often. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
library(targets)
tar_script({
tar_read_data <- function(name, file, reader, ...) {
name_read <- deparse(rlang::enexpr(name))
name_file <- paste0(name_read, "_file")
sym_file <- rlang::sym(name_file)
expr_read <- as.call(rlang::enexprs(reader, sym_file, ...))
list(
targets::tar_target_raw(name_file, file, format = "file"),
targets::tar_target_raw(name_read, expr_read)
)
}
tar_read_data(data, "data.csv", read_csv, skip = 5, col_types = cols())
})
tar_manifest(fields = all_of(c("command", "format")))
#> # A tibble: 2 × 3
#> name command format
#> <chr> <chr> <chr>
#> 1 data_file "\"data.csv\"" file
#> 2 data "read_csv(data_file, skip = 5, col_types = cols())" rds Created on 2022-01-18 by the reprex package (v2.0.1) Could be a nice addition to |
Beta Was this translation helpful? Give feedback.
rlang
has nice functions for this, especiallyenexprs()
.rlang::call2()
almost fits, butbase::as.call()
may be a little better for the situation.