{bookdown} programmatic creation of chapters

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 the data/chapters-data.csv data file. Building in this branch inserts these chapters due to the rmd_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)
image image

Parameterised .Rmd templates

The post-build branch contains the following parameterised .Rmd file, saved as _chapter-template.Rmd as files beginning with _ are skipped:

  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 via
render("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?

Hi @martinjhnhadley,

So I just want to make sure I'm totally following what you hope to achieve. My understanding is you want to be able to, let's say, add a new row to your CSV file and be able to re-render the book using the chapter order in the CSV file? Or do you also want to be able to customize the order at render call-time?


I would generate / modify the bookdown.yaml file from the csv.



I understand you want to generate a Rmd file from a Rmd template with variables. is that right ?
I would use a templating framework for that, like whisker or brew. I think with that you'll be able to fill your Rmd template to a Rmd document that would be named and render as html by bookdown process.
Just sharing idea here... what do you think ?


Are your chapter self-defined?

Bookdown allows to select a K-M or M-K approach. K for knit and M for merge. So I guess you could generated MD files and then use Bookdown to merge them.

So I would try to knit the chapters as single process with the keep_md option. Then see what happen when bookdown.yaml file list these MD files.

I did not try this. Use these information only as food for thoughts.


This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.