R Bookdown

  



Note: This book has been published by Chapman & Hall/CRC. The online version of this book is free to read here (thanks to Chapman & Hall/CRC), and licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

The.bookdown. package is based on R Markdown, and provides output formats for books and long-form articles, including the GitBook format, which is a multi-page HTML output format with a useful and beautiful user interface. It is much easier to typeset in HTML than LaTeX, so you can always preview your book in HTML, and work on PDF after the. For example, we are using the bookdown package (Xie 2021) in this sample book, which was built on top of R Markdown and knitr (Xie 2015). 41.5 How the square bracket links work Context: you prefer to link with text, not a chapter or section number.

The document format “R Markdown” was first introduced in the knitr package (Xie 2015, 2021b) in early 2012. The idea was to embed code chunks (of R or other languages) in Markdown documents. In fact, knitr supported several authoring languages from the beginning in addition to Markdown, including LaTeX, HTML, AsciiDoc, reStructuredText, and Textile. Looking back over the five years, it seems to be fair to say that Markdown has become the most popular document format, which is what we expected. The simplicity of Markdown clearly stands out among these document formats.

However, the original version of Markdown invented by John Gruber was often found overly simple and not suitable to write highly technical documents. For example, there was no syntax for tables, footnotes, math expressions, or citations. Fortunately, John MacFarlane created a wonderful package named Pandoc (http://pandoc.org) to convert Markdown documents (and many other types of documents) to a large variety of output formats. More importantly, the Markdown syntax was significantly enriched. Now we can write more types of elements with Markdown while still enjoying its simplicity.

In a nutshell, R Markdown stands on the shoulders of knitr and Pandoc. The former executes the computer code embedded in Markdown, and converts R Markdown to Markdown. The latter renders Markdown to the output format you want (such as PDF, HTML, Word, and so on).

The rmarkdown package (Allaire, Xie, McPherson, et al. 2021) was first created in early 2014. During the past four years, it has steadily evolved into a relatively complete ecosystem for authoring documents, so it is a good time for us to provide a definitive guide to this ecosystem now. At this point, there are a large number of tasks that you could do with R Markdown:

  • Compile a single R Markdown document to a report in different formats, such as PDF, HTML, or Word.

  • Create notebooks in which you can directly run code chunks interactively.

  • Make slides for presentations (HTML5, LaTeX Beamer, or PowerPoint).

  • Produce dashboards with flexible, interactive, and attractive layouts.

  • Build interactive applications based on Shiny.

  • Write journal articles.

  • Author books of multiple chapters.

  • Generate websites and blogs.

There is a fundamental assumption underneath R Markdown that users should be aware of: we assume it suffices that only a limited number of features are supported in Markdown. By “features,” we mean the types of elements you can create with native Markdown. The limitation is a great feature, not a bug. R Markdown may not be the right format for you if you find these elements not enough for your writing: paragraphs, (section) headers, block quotations, code blocks, (numbered and unnumbered) lists, horizontal rules, tables, inline formatting (emphasis, strikeout, superscripts, subscripts, verbatim, and small caps text), LaTeX math expressions, equations, links, images, footnotes, citations, theorems, proofs, and examples. We believe this list of elements suffice for most technical and non-technical documents. It may not be impossible to support other types of elements in R Markdown, but you may start to lose the simplicity of Markdown if you wish to go that far.

Epictetus once said, “Wealth consists not in having great possessions, but in having few wants.” The spirit is also reflected in Markdown. If you can control your preoccupation with pursuing typesetting features, you should be much more efficient in writing the content and can become a prolific author. It is entirely possible to succeed with simplicity. Jung Jae-sung was a legendary badminton player with a remarkably simple playing style: he did not look like a talented player and was very short compared to other players, so most of the time you would just see him jump three feet off the ground and smash like thunder over and over again in the back court until he beats his opponents.

Please do not underestimate the customizability of R Markdown because of the simplicity of its syntax. In particular, Pandoc templates can be surprisingly powerful, as long as you understand the underlying technologies such as LaTeX and CSS, and are willing to invest time in the appearance of your output documents (reports, books, presentations, and/or websites). As one example, you may check out the PDF report of the 2017 Employer Health Benefits Survey. It looks fairly sophisticated, but was actually produced via bookdown(Xie 2016), which is an R Markdown extension. A custom LaTeX template and a lot of LaTeX tricks were used to generate this report. Not surprisingly, this very book that you are reading right now was also written in R Markdown, and its full source is publicly available in the GitHub repository https://github.com/rstudio/rmarkdown-book.

R Markdown documents are often portable in the sense that they can be compiled to multiple types of output formats. Again, this is mainly due to the simplified syntax of the authoring language, Markdown. The simpler the elements in your document are, the more likely that the document can be converted to different formats. Similarly, if you heavily tailor R Markdown to a specific output format (e.g., LaTeX), you are likely to lose the portability, because not all features in one format work in another format.

Last but not least, your computing results will be more likely to be reproducible if you use R Markdown (or other knitr-based source documents), compared to the manual cut-and-paste approach. This is because the results are dynamically generated from computer source code. If anything goes wrong or needs to be updated, you can simply fix or update the source code, compile the document again, and the results will be automatically updated. You can enjoy reproducibility and convenience at the same time.

Here’s where I park littleexamplesfor myself about bookdown mechanics that I keep forgetting.

The bookdown book: https://bookdown.org/yihui/bookdown/

41.2 About labelling things

R Bookdown

You can label chapter and section titles using {#label} after them, e.g., we can reference Section 41.2. If you do not manually label them, there will be automatic labels anyway, e.g., this reference to the unlabelled heading 41.1 uses the automatically generated label @ref(heading-blah-blah).

41.3 Cross-references

Add an explicit label by adding {#label} to the end of the section header. If you know you’re going to refer to something, this is probably a good idea.

R Bookdown Tutorial

To refer to in a chapter- or section-number-y way, use @ref(label).

  • @ref(install-git) example: In chapter 6 we explain how to install Git.

If you are happy with the section header as the link text, use it inside a single set of square brackets:

R Bookdown
  • [A picture is worth a thousand words]: example “A picture is worth a thousand words” via A picture is worth a thousand words

There are two ways to specify custom link text:

  • [link text][Section header text], e.g., “pic = 1000 words” via pic = 1000 words
  • [link text](#label), e.g., “RStudio, meet Git” via RStudio, meet Git

The Pandoc documentation provides more details on automatic section IDs and implicit header references.

41.4 Figures, tables, citations

Figures and tables with captions will be placed in figure and table environments, respectively.

Figure 41.1: Here is a nice figure!

Reference a figure by its code chunk label with the fig: prefix, e.g., see Figure 41.1. Similarly, you can reference tables generated from knitr::kable(), e.g., see Table 41.1.

Table 41.1: Here is a nice table!
Sepal.LengthSepal.WidthPetal.LengthPetal.WidthSpecies
5.13.51.40.2setosa
4.93.01.40.2setosa
4.73.21.30.2setosa
4.63.11.50.2setosa
5.03.61.40.2setosa
5.43.91.70.4setosa
4.63.41.40.3setosa
5.03.41.50.2setosa
4.42.91.40.2setosa
4.93.11.50.1setosa
5.43.71.50.2setosa
4.83.41.60.2setosa
4.83.01.40.1setosa
4.33.01.10.1setosa
5.84.01.20.2setosa
5.74.41.50.4setosa
5.43.91.30.4setosa
5.13.51.40.3setosa
5.73.81.70.3setosa
5.13.81.50.3setosa

You can write citations, too. For example, we are using the bookdown package (Xie 2021) in this sample book, which was built on top of R Markdown and knitr(Xie 2015).

41.5 How the square bracket links work

Context: you prefer to link with text, not a chapter or section number.

  • GOOD! Here’s a link to Contributors.
  • BAD. You can see contributors in 2.

Facts and vocabulary

  • Each chapter is a file. These files should begin with the chapter title using a level-one header, e.g., # Chapter Title.
  • A chapter can be made up of sections, indicated by lower-level headers, e.g., ## A section within the chapter.
  • There are three ways to address a section when creating links within your book:
    • Explicit identifier: In # My header {#foo} the explicit identifier is foo.
    • Automatically generated identifier: my-header is the auto-identifier for # My header. Pandoc creates auto-identifiers according to rules laid out in Extension: auto_identifiers.
    • The header text, e.g., My header be used verbatim as an implicit header reference. See Extension: implicit_header_references for more.
  • All 3 forms can be used to create cross-references but you build the links differently.
  • Advantage of explicit identification: You are less likely to update the section header and then forget to make matching edits to references elsewhere in the book.

Bookdown Chinese

How to make text-based links using explicit identifiers, automatic identifiers, and implicit references:

Bookdown Example

  • Use implicit reference alone to get a link where the text is exactly the section header:
    • [Introduce yourself to Git]Introduce yourself to Git
    • [Success and operating systems]Success and operating systems
  • You can provide custom text for the link with all 3 methods of addressing a section:
    • Implicit header reference: [link text][Recommended Git clients]link text
    • Explicit identifier: [hello git! I'm Jenny](#hello-git)hello git! I’m Jenny
    • Automatic identifier: [Any text you want](#recommended-git-clients)Any text you want