Targets to render a parameterized R Markdown report with multiple sets of parameters.

tar_render_rep(
  name,
  path,
  params = data.frame(),
  batches = NULL,
  packages = targets::tar_option_get("packages"),
  library = targets::tar_option_get("library"),
  format = targets::tar_option_get("format"),
  iteration = targets::tar_option_get("iteration"),
  error = targets::tar_option_get("error"),
  deployment = targets::tar_option_get("deployment"),
  priority = targets::tar_option_get("priority"),
  resources = targets::tar_option_get("resources"),
  retrieval = targets::tar_option_get("retrieval"),
  cue = targets::tar_option_get("cue"),
  quiet = TRUE,
  ...
)

Arguments

name

Symbol, name of the target.

path

Character string, file path to the R Markdown source file. Must have length 1.

params

Code to generate a data frame or tibble with one row per rendered report and one column per R Markdown parameter. You may also include an output_file column to specify the path of each rendered report.

batches

Number of batches to group the R Markdown files. For a large number of reports, increase the number of batches to decrease target-level overhead. Defaults to the number of reports to render (1 report per batch).

packages

Character vector of packages to load right before the target builds. Use tar_option_set() to set packages globally for all subsequent targets you define.

library

Character vector of library paths to try when loading packages.

format

Optional storage format for the target's return value. With the exception of format = "file", each target gets a file in _targets/objects, and each format is a different way to save and load this file. Possible formats:

  • "rds": Default, uses saveRDS() and readRDS(). Should work for most objects, but slow.

  • "qs": Uses qs::qsave() and qs::qread(). Should work for most objects, much faster than "rds". Optionally set the preset for qsave() through the resources argument, e.g. tar_target(..., resources = list(preset = "archive")).

  • "fst": Uses fst::write_fst() and fst::read_fst(). Much faster than "rds", but the value must be a data frame. Optionally set the compression level for fst::write_fst() through the resources argument, e.g. tar_target(..., resources = list(compress = 100)).

  • "fst_dt": Same as "fst", but the value is a data.table. Optionally set the compression level the same way as for "fst".

  • "fst_tbl": Same as "fst", but the value is a tibble. Optionally set the compression level the same way as for "fst".

  • "keras": Uses keras::save_model_hdf5() and keras::load_model_hdf5(). The value must be a Keras model.

  • "torch": Uses torch::torch_save() and torch::torch_load(). The value must be an object from the torch package such as a tensor or neural network module.

  • "file": A dynamic file. To use this format, the target needs to manually identify or save some data and return a character vector of paths to the data. Then, targets automatically checks those files and cues the appropriate build decisions if those files are out of date. Those paths must point to files or directories, and they must not contain characters | or *. All the files and directories you return must actually exist, or else targets will throw an error. (And if storage is "worker", targets will first stall out trying to wait for the file to arrive over a network file system.)

  • "url": A dynamic input URL. It works like format = "file" except the return value of the target is a URL that already exists and serves as input data for downstream targets. Optionally supply a custom curl handle through the resources argument, e.g. tar_target(..., resources = list(handle = curl::new_handle())). The data file at the URL needs to have an ETag or a Last-Modified time stamp, or else the target will throw an error because it cannot track the data. Also, use extreme caution when trying to use format = "url" to track uploads. You must be absolutely certain the ETag and Last-Modified time stamp are fully updated and available by the time the target's command finishes running. targets makes no attempt to wait for the web server.

  • "aws_rds", "aws_qs", "aws_fst", "aws_fst_dt", "aws_fst_tbl", "aws_keras": AWS-powered versions of the respective formats "rds", "qs", etc. The only difference is that the data file is uploaded to the AWS S3 bucket you supply to resources. See the cloud computing chapter of the manual for details.

  • "aws_file": arbitrary dynamic files on AWS S3. The target should return a path to a temporary local file, then targets will automatically upload this file to an S3 bucket and track it for you. Unlike format = "file", format = "aws_file" can only handle one single file, and that file must not be a directory. tar_read() and downstream targets download the file to _targets/scratch/ locally and return the path. _targets/scratch/ gets deleted at the end of tar_make(). Requires the same resources and other configuration details as the other AWS-powered formats. See the cloud computing chapter of the manual for details.

iteration

Character of length 1, name of the iteration mode of the target. Choices:

  • "vector": branching happens with vctrs::vec_slice() and aggregation happens with vctrs::vec_c().

  • "list", branching happens with [[]] and aggregation happens with list().

  • "group": dplyr::group_by()-like functionality to branch over subsets of a data frame. The target's return value must be a data frame with a special tar_group column of consecutive integers from 1 through the number of groups. Each integer designates a group, and a branch is created for each collection of rows in a group. See the tar_group() function to see how you can create the special tar_group column with dplyr::group_by().

error

Character of length 1, what to do if the target runs into an error. If "stop", the whole pipeline stops and throws an error. If "continue", the error is recorded, but the pipeline keeps going.

deployment

Character of length 1, only relevant to tar_make_clustermq() and tar_make_future(). If "worker", the target builds on a parallel worker. If "main", the target builds on the host machine / process managing the pipeline.

priority

Numeric of length 1 between 0 and 1. Controls which targets get deployed first when multiple competing targets are ready simultaneously. Targets with priorities closer to 1 get built earlier. Only applies to tar_make_future() and tar_make_clustermq() (not tar_make()). tar_make_future() with no extra settings is a drop-in replacement for tar_make() in this case.

resources

A named list of computing resources. Uses:

  • Template file wildcards for future::future() in tar_make_future().

  • Template file wildcards clustermq::workers() in tar_make_clustermq().

  • Custom target-level future::plan(), e.g. resources = list(plan = future.callr::callr).

  • Custom curl handle if format = "url", e.g. resources = list(handle = curl::new_handle(nobody = TRUE)). In custom handles, most users should manually set nobody = TRUE so targets does not download the entire file when it only needs to check the time stamp and ETag.

  • Custom preset for qs::qsave() if format = "qs", e.g. resources = list(handle = "archive").

  • Custom compression level for fst::write_fst() if format is "fst", "fst_dt", or "fst_tbl", e.g. resources = list(compress = 100).

  • AWS bucket and prefix for the "aws_" formats, e.g. resources = list(bucket = "your-bucket", prefix = "folder/name"). bucket is required for AWS formats. See the cloud computing chapter of the manual for details.

retrieval

Character of length 1, only relevant to tar_make_clustermq() and tar_make_future(). If "main", the target's dependencies are loaded on the host machine and sent to the worker before the target builds. If "worker", the worker loads the targets dependencies.

cue

An optional object from tar_cue() to customize the rules that decide whether the target is up to date.

quiet

An option to suppress printing of the pandoc command line.

...

Other named arguments to rmarkdown::render(). Unlike tar_render(), these arguments are evaluated when the target is defined, not when it is run. (The only reason to delay evaluation in tar_render() was to handle R Markdown parameters, and tar_render_rep() handles them differently.)

Value

A list of target objects to render the R Markdown reports. Changes to the parameters, source file, dependencies, etc. will cause the appropriate targets to rerun during tar_make().

Target objects represent skippable steps of the analysis pipeline as described at https://books.ropensci.org/targets/. Please see the design specification at https://books.ropensci.org/targets-design/ to learn about the structure and composition of target objects.

Details

tar_render_rep() is an alternative to tar_target() for parameterized R Markdown reports that depend on other targets. Parameters must be given as a data frame with one row per rendered report and one column per parameter. An optional output_file column may be included to set the output file path of each rendered report. The R Markdown source should mention other dependency targets tar_load() and tar_read() in the active code chunks (which also allows you to render the report outside the pipeline if the _targets/ data store already exists and appropriate defaults are specified for the parameters). (Do not use tar_load_raw() or tar_read_raw() for this.) Then, tar_render() defines a special kind of target. It 1. Finds all the tar_load()/tar_read() dependencies in the report and inserts them into the target's command. This enforces the proper dependency relationships. (Do not use tar_load_raw() or tar_read_raw() for this.) 2. Sets format = "file" (see tar_target()) so targets watches the files at the returned paths and reruns the report if those files change. 3. Configures the target's command to return the output report files: the rendered document, the source file, and then the *_files/ directory if it exists. All these file paths are relative paths so the project stays portable. 4. Forces the report to run in the user's current working directory instead of the working directory of the report. 5. Sets convenient default options such as deployment = "main" in the target and quiet = TRUE in rmarkdown::render().

Examples

if (identical(Sys.getenv("TAR_LONG_EXAMPLES"), "true")) { targets::tar_dir({ # tar_dir() runs code from a temporary directory. # Parameterized R Markdown: lines <- c( "---", "title: 'report.Rmd file'", "output_format: html_document", "params:", " par: \"default value\"", "---", "Assume these lines are in a file called report.Rmd.", "```{r}", "print(params$par)", "```" ) # The following pipeline will run the report for each row of params. targets::tar_script({ library(tarchetypes) list( tar_render_rep( report, "report.Rmd", params = tibble::tibble(par = c(1, 2)) ) ) }, ask = FALSE) # Then, run the targets pipeline as usual. }) }