Search code examples
rr-markdownbookdown

R: Bookdown: One chapter per Markdown File (Rmd)


In "bookdown: Authoring Books and Technical Documents with R Markdown" Yihui Xie (2017) writes:

[...] one chapter lives in one R Markdown file [...]. Each R Markdown file must start immediately with the chapter title using the first-level heading, e.g., # Chapter Title

For practical reasons, I'm do not want to follow this convention. But other than the warning message (see below), I haven't observed any issues as a consequence of my insubordination. Is it OK to just ignore Xie (2017) and the corresponding warning? If so, why are the rules and warnings there in the first place?

Warning message:
In split_chapters(output, gitbook_page, number_sections, split_by,  :
  You have n Rmd input file(s) but only n first-level heading(s). Did you forget first-level headings in certain Rmd files?

Solution

  • When you build the book, bookdown merges the separate .Rmd files into a single Rmd file, and then knitr is run on this master file. Where it joins files it inserts a comment to show the start of the next file such as:

    Content from chapter 1.
    
    <!--chapter:end:index.Rmd-->
    
    # Introduction {#intro}
    

    As these comments are just comments, there is no technical reason that the following chapter must start with the # as required.

    I suggest this is more of a styling thing, just like it is conventional to indent code, put spaces after a comma etc. One of the greatest benefits of RMarkdown is how easy it is to understand a file without having to know all the syntax, but lacking a title could confuse a potential reader. However, as you said, particular uses might make sense.