-
I'm trying to write a target factory that will produce a target for an arbitrary function call and list of arguments, which are other targets. Here's a complete working example #_targets.R
library(targets)
generate_some_data = function() {
rnorm(100)
}
# target factory
load_custom = function(tname, func, args) {
l = as.list(args)
command_data = substitute(do.call(func, args), env=list(args=lapply(l, as.symbol),
func=as.symbol(func)))
tar_target_raw(tname, command_data)
}
list(
tar_target(tar1, generate_some_data()),
tar_target(tar2, generate_some_data()),
tar_target(tar3, sum(tar1, tar2)),
tar_target(tar4, do.call(sum, list(tar1, tar2))),
load_custom("tar5", "sum", list("tar1", "tar2"))
) In this example, the creation of tar_make()
● run target tar5
✖ error target tar5
Error : object 'tar1' not found This indicates that it is not correctly identifying |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
View(load_custom("tar5", "sum", list("tar1", "tar2"))$command$expr) To avoid this problem, I would create an expression representation of load_custom = function(tname, func, args) {
args_expr = as.call(c(as.symbol("list"), lapply(args, as.symbol)))
command_data = substitute(
do.call(func, args),
env = list(args = args_expr, func = as.symbol(func))
)
tar_target_raw(tname, command_data)
} |
Beta Was this translation helpful? Give feedback.
tar5
is disconnected from the graph because part of the R command is actually a list.To avoid this problem, I would create an expression representation of
args
instead of insertingargs
directly viasubstitute()
. Elements ofenv
should be symbols or expressions. The following works for me: