{targets} and {tarchetypes} make it straightforward to include individual
"dependency-aware R Markdown reports inside the individual targets of a pipeline"
But a {bookdown}-based approach can be tricky. This function creates
targets to let you concisely (e.g. tar_bookdown("report")) include a
bookdown report in your pipeline, letting targets do its magic handling
dependencies.
tar_bookdown(
input_dir = "report",
input_files = ".",
output_dir = NULL,
output_format = NULL,
preview = FALSE
)the main directory of the book
character vector with input files, in case you do not want to render them all (the default)
The output directory. If NULL, a field named
output_dir in the configuration file _bookdown.yml will be
used (possibly not specified, either, in which case a directory name
_book will be used).
as in bookdown::render_book
Whether to render and preview the input files specified by the
input argument. Previewing a certain chapter may save compilation
time as you actively work on this chapter, but the output may not be
accurate (e.g. cross-references to other chapters will not work).
a list of targets, including one target for each .Rmd file in input_dir and one target for the bookdown output, that depends on all .Rmd files
So, this function
receives input_dir, typically a subfolder in your project that includes
all .Rmd files and {bookdown} config files
creates one target per .Rmd file in input_dir, making sure to include its
dependencies (e.g. tar_read, tar_load calls within .Rmd files, detected via
tarchetypes::tar_knitr_deps)
includes also a single target for the {bookdown} report, that depends on
all the individual targets corresponding to .Rmd files
returns all the targets above as a list to be included in your pipeline (please note all targets are format = "file")
Note that when running your pipeline, the bookdown target renders the book
using flowme::bookme which is simply a wrapper around
bookdown::render_book to deal with {bookdown}'s restrictions and be able
to render in subdirectories. This collides with the stricter (compared to
.drake) policy of {targets} to find the data store (_targets/) at the
project root.
EDIT: now it seems {targets} is more flexible and you can
configure the location using tar_config_set(). Yet, the discussion below
still applies because we need to deal with temporarily changing working
directory to make bookdown work).
flowme::bookme changes working directory to input_dir to render the book.
Thus, when you call tar_read in an .Rmd, {targets} will look for the data
store in input_dir and not in your project root where it probably lies,
leading your pipeline to fail. To circumvent this issue, there are a few
alternatives you can use when retrieving targets in your .Rmd.
change the store directly to a hard-coded value, which is probably the less verbose alternative, but it is not robust or portable (e.g. if you nest the bookdown folder into other directories)
tar_read(data, store = "..")
also change the store, but using here to help you find the data store
tar_read(data, store = here::here(targets::tar_config_get("store")))
change the working directory again, temporarily
xfun::in_dir(here::here(), tar_read(data)) # or withr::with_dir
TODO: there might be a better way to do it. Try and find it. Meanwhile, this works.
if (FALSE) {
tar_bookdown("report")
}