Example repository for rendering a bookdown book, compiling parameterized Rmarkdown documents, using the targets package.
This example extends the targets-minimal example. To mimic a directory of input files, we split the example data into two chunks.
library(tidyverse)
data <- read_csv('data/raw_data.csv')
nrows <- 50
data1 <- head(data, n = nrows)
data2 <- tail(data, n = nrows)
write_csv(data1, 'data/split/data1.csv')
write_csv(data2, 'data/split/data2.csv')
To setup a target for our input files, we use tar_files
from
tarchetypes (target: paths). Dynamic branching in
subsequent targets is mapped over these files (targets: raw_data, data, hist, fit).
Parameterized RMarkdown documents are generated by mapping over the fitted model
and histograms using a template document (template/template.Rmd
) (target: report).
Finally, the output documents are merged into a book with bookdown. We use a
custom wrapper function render_with_deps
to explicitly declare the book's
dependency on the output reports (target: book).
-
We need to change the extension of the parameterized github_document outputs (target: report) from
.md
to.Rmd
becausebookdown
doesn't pick up.md
files unless they are explicitly listed with thermd_files
argument in_bookdown.yml
. See here. -
The
title
andoutput_file
for rendered RMarkdown documents are set in this example to the name of the branch (target: report). If adapted for your own use, this could alternatively be set to an id from the data, etc. -
We can't use the
tar_render
function fromtarchetypes
because we need to callbookdown::render_book
and notrmarkdown::render
(target: book). Therefore, we need to track the.Rmd
files in separate targets. -
You may need to use
pandoc
>= 2.11.2 when generating the github_documents (target: report) so ATX style headings are used by default.