In the command of each target, wrap each mention of each dependency target in an arbitrary R expression.
tar_hook_inner() expects unevaluated expressions for the hook and
names arguments, whereas tar_hook_inner_raw() expects
evaluated expression objects.
Usage
tar_hook_inner(
targets,
hook,
names = NULL,
names_wrap = NULL,
set_deps = TRUE,
envir = parent.frame()
)
tar_hook_inner_raw(
targets,
hook,
names = NULL,
names_wrap = NULL,
set_deps = TRUE,
envir = parent.frame()
)Arguments
- targets
A list of target definition objects. The input target list can be arbitrarily nested, but it must consist entirely of target objects. In addition, the return value is a simple list where each element is a target definition object. All hook functions remove the nested structure of the input target list.
- hook
R code to wrap each target's command. The hook must contain the special placeholder symbol
.xsotar_hook_inner()knows where to insert the code to wrap mentions of dependencies.tar_hook_inner()expects unevaluated expressions for thehookandnamesarguments, whereastar_hook_inner_raw()expects evaluated expression objects.- names
Name of targets in the target list to apply the hook. Supplied using
tidyselecthelpers likestarts_with(), as innames = starts_with("your_prefix_"). Set toNULLto include all targets supplied to thetargetsargument. Targets not included innamesstill remain in the target list, but they are not modified because the hook does not apply to them.The regular hook functions expects unevaluated expressions for the
hookandnamesarguments, whereas the"_raw"versions expect evaluated expression objects.- names_wrap
Names of targets to wrap with the hook where they appear as dependencies in the commands of other targets. Use
tidyselecthelpers likestarts_with(), as innames_wrap = starts_with("your_prefix_").- set_deps
Logical of length 1, whether to refresh the dependencies of each modified target by scanning the newly generated target commands for dependencies. If
FALSE, then the target will keep the original set of dependencies it had before the hook. Set toNULLto include all targets supplied to thetargetsargument.TRUEis recommended for nearly all situations. Only useFALSEif you have a specialized use case and you know what you are doing.- envir
Optional environment to construct the quosure for the
namesargument to select names.
Value
A flattened list of target definition objects with the hooks applied. Even if the input target list had a nested structure, the return value is a simple list where each element is a target definition object. All hook functions remove the nested structure of the input target list.
Details
The expression you supply to hook
must contain the special placeholder symbol .x
so tar_hook_inner() knows where to insert the original command
of the target.
Target definition objects
Most tarchetypes functions are target factories,
which means they return target definition objects
or lists of target definition objects.
target definition objects represent
skippable steps of the analysis pipeline
as described at https://books.ropensci.org/targets/.
Please read the walkthrough at
https://books.ropensci.org/targets/walkthrough.html
to understand the role of target definition
objects in analysis pipelines.
For developers, https://wlandau.github.io/targetopia/contributing.html#target-factories explains target factories (functions like this one which generate targets) and the design specification at https://books.ropensci.org/targets-design/ details the structure and composition of target definition objects.
See also
Other hooks:
tar_hook_before(),
tar_hook_outer()
Examples
if (identical(Sys.getenv("TAR_LONG_EXAMPLES"), "true")) {
targets::tar_dir({ # tar_dir() runs code from a temporary directory.
targets::tar_script({
targets <- list(
# Nested target lists work with hooks.
list(
targets::tar_target(x1, task1()),
targets::tar_target(x2, task2(x1))
),
targets::tar_target(x3, task3(x2, x1)),
targets::tar_target(y1, task4(x3))
)
tarchetypes::tar_hook_inner(
targets = targets,
hook = fun(.x),
names = starts_with("x")
)
})
targets::tar_manifest(fields = command)
# With tar_hook_inner_raw():
targets::tar_script({
targets <- list(
# Nested target lists work with hooks.
list(
targets::tar_target(x1, task1()),
targets::tar_target(x2, task2(x1))
),
targets::tar_target(x3, task3(x2, x1)),
targets::tar_target(y1, task4(x3))
)
tarchetypes::tar_hook_inner_raw(
targets = targets,
hook = quote(fun(.x)),
names = quote(starts_with("x"))
)
})
})
}