I'm looking to generate a {bookdown} book where chapters are generated from a .csv
file, which requires two steps:
- Parameterised
.Rmd
templates - Customising the build order of the book
To demonstrate what I'd like to build take a look at this github repo which is split into two branches:
- master: branch contains only those chapters that exist as distinct
.Rmd
files - post-building: branch contains
.Rmd
files for the chapters that are defined in thedata/chapters-data.csv
data file. Building in this branch inserts these chapters due to thermd_files
parameter in the_bookdown.yml
file.
Here are screenshots of how the chapters appear in
head branch (without data/chapters-data.csv) |
post-building branch (with data/chapters-data.csv) |
---|---|
![]() |
![]() |
Parameterised .Rmd
templates
The post-build
branch contains the following parameterised .Rmd file, saved as _chapter-template.Rmd
as files beginning with _
are skipped:
---
output:
bookdown::gitbook
params:
chapter_subject: "Chapter title missing"
chapter_strapline: "Chapter strap line missing"
chapter_body: "Chapter body missing"
---
# `r params$chapter_subject` {-}
> `r params$chapter_strapline`
`r params$chapter_body`
This template works, it's all good.
Customising the build order of the book
There appear to be two solutions:
a) Generate new .Rmd files on the fly |
b) use rmd_files() |
---|---|
1. Hijack the build process | 1. Hijack the build process |
2. Generate output .html files viarender("template.Rmd", params = list()) |
2. Generate intermediate .Rmd files by mapping the template over the parameter values |
3. Set the chapter order by renaming all .Rmd and .html files, eg: 01-XX.Rmd , 02-YY.Rmd |
3. Set the chapter order via the rmd_files parameter of _bookdown.yml |
4. Allow the build to continue as normal | 4. Allow the build to continue as normal |
I think that solution B is slightly more elegant, however I don't know how to generate the intermediary .Rmd files that I would need to reference in _bookdown.yml
.
However, regardless of the method - how can I hijack the {bookdown}
build process to add content?