| target {drake} | R Documentation |
Customize a target in drake_plan().
![[Stable]](../help/figures/lifecycle-stable.svg)
Description
The target() function is a way to
configure individual targets in a drake plan.
Its most common use is to invoke static branching
and dynamic branching, and it can also set the values
of custom columns such as format, elapsed, retries,
and max_expand. Details are at
https://books.ropensci.org/drake/plans.html#special-columns.
Note: drake_plan(my_target = my_command())
is equivalent to
drake_plan(my_target = target(my_command()).
Usage
target(command = NULL, transform = NULL, dynamic = NULL, ...)
Arguments
command |
The command to build the target. |
transform |
A call to |
dynamic |
A call to |
... |
Optional columns of the plan for a given target.
See the Columns section of this help file for a selection
of special columns that |
Details
target() must be called inside drake_plan().
It is invalid otherwise.
Value
A one-row workflow plan data frame with the named arguments as columns.
Columns
drake_plan() creates a special data frame. At minimum, that data frame
must have columns target and command with the target names and the
R code chunks to build them, respectively.
You can add custom columns yourself, either with target() (e.g.
drake_plan(y = target(f(x), transform = map(c(1, 2)), format = "fst")))
or by appending columns post-hoc (e.g. plan$col <- vals).
Some of these custom columns are special. They are optional,
but drake looks for them at various points in the workflow.
-
transform: a call tomap(),split(),cross(), orcombine()to create and manipulate large collections of targets. Details: (https://books.ropensci.org/drake/plans.html#large-plans). # nolint -
format: set a storage format to save big targets more efficiently. See the "Formats" section of this help file for more details. -
trigger: rule to decide whether a target needs to run. It is recommended that you define this one withtarget(). Details:https://books.ropensci.org/drake/triggers.html. -
hpc: logical values (TRUE/FALSE/NA) whether to send each target to parallel workers. Visithttps://books.ropensci.org/drake/hpc.html#selectivityto learn more. -
resources: target-specific lists of resources for a computing cluster. Seehttps://books.ropensci.org/drake/hpc.html#advanced-optionsfor details. -
caching: overrides thecachingargument ofmake()for each target individually. Possible values:"main": tell the main process to store the target in the cache.
"worker": tell the HPC worker to store the target in the cache.
NA: default to the
cachingargument ofmake().
-
elapsedandcpu: number of seconds to wait for the target to build before timing out (elapsedfor elapsed time andcpufor CPU time). -
retries: number of times to retry building a target in the event of an error. -
seed: an optional pseudo-random number generator (RNG) seed for each target.drakeusually comes up with its own unique reproducible target-specific seeds using the global seed (theseedargument tomake()anddrake_config()) and the target names, but you can overwrite these automatic seeds.NAentries default back todrake's automatic seeds. -
max_expand: for dynamic branching only. Same as themax_expandargument ofmake(), but on a target-by-target basis. Limits the number of sub-targets created for a given target.
Keywords
drake_plan() understands special keyword functions for your commands.
With the exception of target(), each one is a proper function
with its own help file.
-
target(): give the target more than just a command. Usingtarget(), you can apply a transformation (examples:https://books.ropensci.org/drake/plans.html#large-plans), # nolint supply a trigger (https://books.ropensci.org/drake/triggers.html), # nolint or set any number of custom columns. -
file_in(): declare an input file dependency. -
file_out(): declare an output file to be produced when the target is built. -
knitr_in(): declare aknitrfile dependency such as an R Markdown (*.Rmd) or R LaTeX (*.Rnw) file. -
ignore(): forcedraketo entirely ignore a piece of code: do not track it for changes and do not analyze it for dependencies. -
no_deps(): telldraketo not track the dependencies of a piece of code.drakestill tracks the code itself for changes. -
id_chr(): Get the name of the current target. -
drake_envir(): get the environment where drake builds targets. Intended for advanced custom memory management.
Formats
Specialized target formats increase efficiency and flexibility.
Some allow you to save specialized objects like keras models,
while others increase the speed while conserving storage and memory.
You can declare target-specific formats in the plan
(e.g. drake_plan(x = target(big_data_frame, format = "fst")))
or supply a global default format for all targets in make().
Either way, most formats have specialized installation requirements
(e.g. R packages) that are not installed with drake by default.
You will need to install them separately yourself.
Available formats:
-
"file": Dynamic files. To use this format, simply create local files and directories yourself and then return a character vector of paths as the target's value. Then,drakewill watch for changes to those files in subsequent calls tomake(). This is a more flexible alternative tofile_in()andfile_out(), and it is compatible with dynamic branching. Seehttps://github.com/ropensci/drake/pull/1178for an example. -
"fst": save big data frames fast. Requires thefstpackage. Note: this format strips non-data-frame attributes such as the -
"fst_tbl": Like"fst", but fortibbleobjects. Requires thefstandtibblepackages. Strips away non-data-frame non-tibble attributes. -
"fst_dt": Like"fst"format, but fordata.tableobjects. Requires thefstanddata.tablepackages. Strips away non-data-frame non-data-table attributes. -
"diskframe": Storesdisk.frameobjects, which could potentially be larger than memory. Requires thefstanddisk.framepackages. Coerces objects todisk.frames. Note:disk.frameobjects get moved to thedrakecache (a subfolder of.drake/for most workflows). To ensure this data transfer is fast, it is best to save yourdisk.frameobjects to the same physical storage drive as thedrakecache,as.disk.frame(your_dataset, outdir = drake_tempfile()). -
"keras": save Keras models as HDF5 files. Requires thekeraspackage. -
"qs": save any R object that can be properly serialized with theqspackage. Requires theqspackage. Usesqsave()andqread(). Uses the default settings inqsversion 0.20.2. -
"rds": save any R object that can be properly serialized. Requires R version >= 3.5.0 due to ALTREP. Note: the"rds"format uses gzip compression, which is slow."qs"is a superior format.
See Also
Examples
# Use target() to create your own custom columns in a drake plan.
# See ?triggers for more on triggers.
drake_plan(
website_data = target(
download_data("www.your_url.com"),
trigger = "always",
custom_column = 5
),
analysis = analyze(website_data)
)
models <- c("glm", "hierarchical")
plan <- drake_plan(
data = target(
get_data(x),
transform = map(x = c("simulated", "survey"))
),
analysis = target(
analyze_data(data, model),
transform = cross(data, model = !!models, .id = c(x, model))
),
summary = target(
summarize_analysis(analysis),
transform = map(analysis, .id = c(x, model))
),
results = target(
bind_rows(summary),
transform = combine(summary, .by = data)
)
)
plan
if (requireNamespace("styler", quietly = TRUE)) {
print(drake_plan_source(plan))
}