R Markdown: The Definitive Guide: Yihui Xie, J. J. Allaire, Garrett Grolemund
R Markdown: The Definitive Guide: Yihui Xie, J. J. Allaire, Garrett Grolemund
Preface xxiii
I Get Started 1
1 Installation 5
2 Basics 7
2.1 Example applications . . . . . . . . . . . . . . . . . . . . . 7
2.1.1 Airbnb’s knowledge repository . . . . . . . . . . . . 7
2.1.2 Homework assignments on RPubs . . . . . . . . . . 7
2.1.3 Personalized mail . . . . . . . . . . . . . . . . . . . 8
2.1.4 2017 Employer Health Benefits Survey . . . . . . . . 8
2.1.5 Journal articles . . . . . . . . . . . . . . . . . . . . . 8
2.1.6 Dashboards at eelloo . . . . . . . . . . . . . . . . . 8
2.1.7 Books . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.1.8 Websites . . . . . . . . . . . . . . . . . . . . . . . . 8
2.2 Compile an R Markdown document . . . . . . . . . . . . . 8
2.3 Cheat sheets . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.4 Output formats . . . . . . . . . . . . . . . . . . . . . . . . 9
2.5 Markdown syntax . . . . . . . . . . . . . . . . . . . . . . . 9
2.5.1 Inline formatting . . . . . . . . . . . . . . . . . . . 9
2.5.2 Block-level elements . . . . . . . . . . . . . . . . . 9
2.5.3 Math expressions . . . . . . . . . . . . . . . . . . . 9
2.6 R code chunks and inline R code . . . . . . . . . . . . . . . 10
2.6.1 Figures . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.6.2 Tables . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.7 Other language engines . . . . . . . . . . . . . . . . . . . . 10
vii
viii Contents
2.7.1 Python . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.7.2 Shell scripts . . . . . . . . . . . . . . . . . . . . . . 10
2.7.3 SQL . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.7.4 Rcpp . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.7.5 Stan . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.7.6 JavaScript and CSS . . . . . . . . . . . . . . . . . . 11
2.7.7 Julia . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.7.8 C and Fortran . . . . . . . . . . . . . . . . . . . . . 11
2.8 Interactive documents . . . . . . . . . . . . . . . . . . . . . 11
2.8.1 HTML widgets . . . . . . . . . . . . . . . . . . . . 11
2.8.2 Shiny documents . . . . . . . . . . . . . . . . . . . 12
II Output Formats 13
3 Documents 17
3.1 HTML document . . . . . . . . . . . . . . . . . . . . . . . 17
3.1.1 Table of contents . . . . . . . . . . . . . . . . . . . . 17
3.1.2 Section numbering . . . . . . . . . . . . . . . . . . 17
3.1.3 Tabbed sections . . . . . . . . . . . . . . . . . . . . 17
3.1.4 Appearance and style . . . . . . . . . . . . . . . . . 18
3.1.5 Figure options . . . . . . . . . . . . . . . . . . . . . 18
3.1.6 Data frame printing . . . . . . . . . . . . . . . . . . 18
3.1.7 Code folding . . . . . . . . . . . . . . . . . . . . . . 18
3.1.8 MathJax equations . . . . . . . . . . . . . . . . . . . 19
3.1.9 Document dependencies . . . . . . . . . . . . . . . 19
3.1.10 Advanced customization . . . . . . . . . . . . . . . 19
3.1.11 Shared options . . . . . . . . . . . . . . . . . . . . . 19
3.1.12 HTML fragments . . . . . . . . . . . . . . . . . . . 20
3.2 Notebook . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.2.1 Using Notebooks . . . . . . . . . . . . . . . . . . . 20
3.2.2 Saving and sharing . . . . . . . . . . . . . . . . . . 21
3.2.3 Notebook format . . . . . . . . . . . . . . . . . . . 21
3.3 PDF document . . . . . . . . . . . . . . . . . . . . . . . . . 22
3.3.1 Table of contents . . . . . . . . . . . . . . . . . . . . 22
3.3.2 Figure options . . . . . . . . . . . . . . . . . . . . . 22
3.3.3 Data frame printing . . . . . . . . . . . . . . . . . . 22
3.3.4 Syntax highlighting . . . . . . . . . . . . . . . . . . 23
3.3.5 LaTeX options . . . . . . . . . . . . . . . . . . . . . 23
3.3.6 LaTeX packages for citations . . . . . . . . . . . . . 23
3.3.7 Advanced customization . . . . . . . . . . . . . . . 23
Contents ix
4 Presentations 27
4.1 ioslides presentation . . . . . . . . . . . . . . . . . . . . . . 27
4.1.1 Display modes . . . . . . . . . . . . . . . . . . . . . 27
4.1.2 Incremental bullets . . . . . . . . . . . . . . . . . . 27
4.1.3 Visual appearance . . . . . . . . . . . . . . . . . . . 27
4.1.4 Code highlighting . . . . . . . . . . . . . . . . . . . 28
4.1.5 Adding a logo . . . . . . . . . . . . . . . . . . . . . 28
4.1.6 Tables . . . . . . . . . . . . . . . . . . . . . . . . . 28
4.1.7 Advanced layout . . . . . . . . . . . . . . . . . . . 28
4.1.8 Text color . . . . . . . . . . . . . . . . . . . . . . . 28
4.1.9 Presenter mode . . . . . . . . . . . . . . . . . . . . 29
4.1.10 Printing and PDF output . . . . . . . . . . . . . . . 29
4.1.11 Custom templates . . . . . . . . . . . . . . . . . . . 29
4.1.12 Other features . . . . . . . . . . . . . . . . . . . . . 29
4.2 Slidy presentation . . . . . . . . . . . . . . . . . . . . . . . 29
4.2.1 Display modes . . . . . . . . . . . . . . . . . . . . . 29
4.2.2 Text size . . . . . . . . . . . . . . . . . . . . . . . . 29
4.2.3 Footer elements . . . . . . . . . . . . . . . . . . . . 30
4.2.4 Other features . . . . . . . . . . . . . . . . . . . . . 30
4.3 Beamer presentation . . . . . . . . . . . . . . . . . . . . . . 30
4.3.1 Themes . . . . . . . . . . . . . . . . . . . . . . . . . 30
4.3.2 Slide level . . . . . . . . . . . . . . . . . . . . . . . 30
4.3.3 Other features . . . . . . . . . . . . . . . . . . . . . 30
4.4 PowerPoint presentation . . . . . . . . . . . . . . . . . . . 31
4.4.1 Custom templates . . . . . . . . . . . . . . . . . . . 31
4.4.2 Other features . . . . . . . . . . . . . . . . . . . . . 31
III Extensions 33
x Contents
5 Dashboards 37
5.1 Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
5.1.1 Row-based layouts . . . . . . . . . . . . . . . . . . 37
5.1.2 Attributes on sections . . . . . . . . . . . . . . . . . 37
5.1.3 Multiple pages . . . . . . . . . . . . . . . . . . . . . 37
5.1.4 Story boards . . . . . . . . . . . . . . . . . . . . . . 38
5.2 Components . . . . . . . . . . . . . . . . . . . . . . . . . . 38
5.2.1 Value boxes . . . . . . . . . . . . . . . . . . . . . . 38
5.2.2 Gauges . . . . . . . . . . . . . . . . . . . . . . . . . 38
5.2.3 Text annotations . . . . . . . . . . . . . . . . . . . . 38
5.2.4 Navigation bar . . . . . . . . . . . . . . . . . . . . . 38
5.3 Shiny . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
5.3.1 Getting started . . . . . . . . . . . . . . . . . . . . . 39
5.3.2 A Shiny dashboard example . . . . . . . . . . . . . 39
5.3.3 Input sidebar . . . . . . . . . . . . . . . . . . . . . 39
5.3.4 Learning more . . . . . . . . . . . . . . . . . . . . . 39
6 Tufte Handouts 41
6.1 Headings . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
6.2 Figures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
6.2.1 Margin figures . . . . . . . . . . . . . . . . . . . . . 41
6.2.2 Arbitrary margin content . . . . . . . . . . . . . . . 41
6.2.3 Full-width figures . . . . . . . . . . . . . . . . . . . 42
6.2.4 Main column figures . . . . . . . . . . . . . . . . . 42
6.3 Sidenotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
6.4 References . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
6.5 Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
6.6 Block quotes . . . . . . . . . . . . . . . . . . . . . . . . . . 43
6.7 Responsiveness . . . . . . . . . . . . . . . . . . . . . . . . 43
6.8 Sans-serif fonts and epigraphs . . . . . . . . . . . . . . . . 43
6.9 Customize CSS styles . . . . . . . . . . . . . . . . . . . . . 43
7 xaringan Presentations 45
7.1 Get started . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
7.2 Keyboard shortcuts . . . . . . . . . . . . . . . . . . . . . . 45
7.3 Slide formatting . . . . . . . . . . . . . . . . . . . . . . . . 45
7.3.1 Slides and properties . . . . . . . . . . . . . . . . . 45
7.3.2 The title slide . . . . . . . . . . . . . . . . . . . . . 46
7.3.3 Content classes . . . . . . . . . . . . . . . . . . . . 46
7.3.4 Incremental slides . . . . . . . . . . . . . . . . . . . 46
Contents xi
8 reveal.js Presentations 49
8.1 Display modes . . . . . . . . . . . . . . . . . . . . . . . . . 49
8.2 Appearance and style . . . . . . . . . . . . . . . . . . . . . 49
8.2.1 Smaller text . . . . . . . . . . . . . . . . . . . . . . 49
8.3 Slide transitions . . . . . . . . . . . . . . . . . . . . . . . . 49
8.4 Slide backgrounds . . . . . . . . . . . . . . . . . . . . . . . 50
8.5 2-D presentations . . . . . . . . . . . . . . . . . . . . . . . 50
8.6 Custom CSS . . . . . . . . . . . . . . . . . . . . . . . . . . 50
8.6.1 Slide IDs and classes . . . . . . . . . . . . . . . . . 50
8.6.2 Styling text spans . . . . . . . . . . . . . . . . . . . 50
8.7 reveal.js options . . . . . . . . . . . . . . . . . . . . . . . . 50
8.8 reveal.js plugins . . . . . . . . . . . . . . . . . . . . . . . . 51
8.9 Other features . . . . . . . . . . . . . . . . . . . . . . . . . 51
9 Community Formats 53
9.1 Lightweight Pretty HTML Documents . . . . . . . . . . . . 53
9.1.1 Usage . . . . . . . . . . . . . . . . . . . . . . . . . 53
9.1.2 Package vignettes . . . . . . . . . . . . . . . . . . . 53
9.2 The rmdformats package . . . . . . . . . . . . . . . . . . . 53
9.3 Shower presentations . . . . . . . . . . . . . . . . . . . . . 54
10 Websites 55
10.1 Get started . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
10.2 The directory structure . . . . . . . . . . . . . . . . . . . . 55
10.3 Deployment . . . . . . . . . . . . . . . . . . . . . . . . . . 55
10.4 Other site generators . . . . . . . . . . . . . . . . . . . . . 55
10.5 rmarkdown’s site generator . . . . . . . . . . . . . . . . . . 56
10.5.1 A simple example . . . . . . . . . . . . . . . . . . . 56
xii Contents
12 Books 61
12.1 Get started . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
12.2 Project structure . . . . . . . . . . . . . . . . . . . . . . . . 61
12.2.1 Index file . . . . . . . . . . . . . . . . . . . . . . . . 61
12.2.2 Rmd files . . . . . . . . . . . . . . . . . . . . . . . . 61
12.2.3 _bookdown.yml . . . . . . . . . . . . . . . . . . . . 62
12.2.4 _output.yml . . . . . . . . . . . . . . . . . . . . . . 62
12.3 Markdown extensions . . . . . . . . . . . . . . . . . . . . . 62
12.3.1 Number and reference equations . . . . . . . . . . . 62
12.3.2 Theorems and proofs . . . . . . . . . . . . . . . . . 62
12.3.3 Special headers . . . . . . . . . . . . . . . . . . . . 62
12.3.4 Text references . . . . . . . . . . . . . . . . . . . . . 62
12.3.5 Cross referencing . . . . . . . . . . . . . . . . . . . 62
12.4 Output Formats . . . . . . . . . . . . . . . . . . . . . . . . 63
12.4.1 HTML . . . . . . . . . . . . . . . . . . . . . . . . . 63
12.4.2 LaTeX/PDF . . . . . . . . . . . . . . . . . . . . . . 63
12.4.3 E-books . . . . . . . . . . . . . . . . . . . . . . . . 63
12.4.4 A single document . . . . . . . . . . . . . . . . . . 63
12.5 Editing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
12.5.1 Build the book . . . . . . . . . . . . . . . . . . . . . 63
12.5.2 Preview a chapter . . . . . . . . . . . . . . . . . . . 64
12.5.3 Serve the book . . . . . . . . . . . . . . . . . . . . . 64
Contents xiii
13 Journals 65
13.1 Get started . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
13.2 rticles templates . . . . . . . . . . . . . . . . . . . . . . . . 65
13.3 Using a template . . . . . . . . . . . . . . . . . . . . . . . . 65
13.4 LaTeX content . . . . . . . . . . . . . . . . . . . . . . . . . 66
13.5 Linking with bookdown . . . . . . . . . . . . . . . . . . . 66
13.6 Contributing templates . . . . . . . . . . . . . . . . . . . . 66
14 Interactive Tutorials 67
14.1 Get started . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
14.2 Tutorial types . . . . . . . . . . . . . . . . . . . . . . . . . 67
14.3 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
14.3.1 Solutions . . . . . . . . . . . . . . . . . . . . . . . . 68
14.3.2 Hints . . . . . . . . . . . . . . . . . . . . . . . . . . 68
14.4 Quiz questions . . . . . . . . . . . . . . . . . . . . . . . . . 68
14.5 Videos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
14.6 Shiny components . . . . . . . . . . . . . . . . . . . . . . . 68
14.7 Navigation and progress tracking . . . . . . . . . . . . . . 69
IV Other Topics 71
15 Parameterized reports 75
15.1 Declaring parameters . . . . . . . . . . . . . . . . . . . . . 75
15.2 Using parameters . . . . . . . . . . . . . . . . . . . . . . . 75
15.3 Knitting with parameters . . . . . . . . . . . . . . . . . . . 75
15.3.1 The Knit button . . . . . . . . . . . . . . . . . . . . 75
15.3.2 Knit with custom parameters . . . . . . . . . . . . . 76
15.3.3 The interactive user interface . . . . . . . . . . . . . 76
15.4 Publishing . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
16 HTML Widgets 77
16.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
16.2 A widget example (sigma.js) . . . . . . . . . . . . . . . . . 77
16.2.1 File layout . . . . . . . . . . . . . . . . . . . . . . . 77
16.2.2 Dependencies . . . . . . . . . . . . . . . . . . . . . 77
xiv Contents
16.2.3 R binding . . . . . . . . . . . . . . . . . . . . . . . 78
16.2.4 JavaScript binding . . . . . . . . . . . . . . . . . . . 78
16.2.5 Demo . . . . . . . . . . . . . . . . . . . . . . . . . . 78
16.3 Creating your own widgets . . . . . . . . . . . . . . . . . . 78
16.3.1 Requirements . . . . . . . . . . . . . . . . . . . . . 78
16.3.2 Scaffolding . . . . . . . . . . . . . . . . . . . . . . . 78
16.3.3 Other packages . . . . . . . . . . . . . . . . . . . . 78
16.4 Widget sizing . . . . . . . . . . . . . . . . . . . . . . . . . 79
16.4.1 Specifying a sizing policy . . . . . . . . . . . . . . . 79
16.4.2 JavaScript resize method . . . . . . . . . . . . . . . 79
16.5 Advanced topics . . . . . . . . . . . . . . . . . . . . . . . . 79
16.5.1 Data transformation . . . . . . . . . . . . . . . . . . 79
16.5.2 Passing JavaScript functions . . . . . . . . . . . . . 80
16.5.3 Custom widget HTML . . . . . . . . . . . . . . . . 80
16.5.4 Create a widget without an R package . . . . . . . . 80
17 Document Templates 81
17.1 Template structure . . . . . . . . . . . . . . . . . . . . . . . 81
17.2 Supporting files . . . . . . . . . . . . . . . . . . . . . . . . 81
17.3 Custom Pandoc templates . . . . . . . . . . . . . . . . . . 81
17.4 Sharing your templates . . . . . . . . . . . . . . . . . . . . 82
19 Shiny Documents 85
19.1 Getting started . . . . . . . . . . . . . . . . . . . . . . . . . 85
19.2 Deployment . . . . . . . . . . . . . . . . . . . . . . . . . . 85
19.2.1 ShinyApps.io . . . . . . . . . . . . . . . . . . . . . 85
19.2.2 Shiny Server / RStudio Connect . . . . . . . . . . . 86
19.3 Embedded Shiny apps . . . . . . . . . . . . . . . . . . . . 86
19.3.1 Inline applications . . . . . . . . . . . . . . . . . . . 86
19.3.2 External applications . . . . . . . . . . . . . . . . . 86
19.4 Shiny widgets . . . . . . . . . . . . . . . . . . . . . . . . . 86
19.4.1 The shinyApp() function . . . . . . . . . . . . . . . 86
19.4.2 Example: k-Means clustering . . . . . . . . . . . . . 87
19.4.3 Widget size and layout . . . . . . . . . . . . . . . . 87
19.5 Multiple pages . . . . . . . . . . . . . . . . . . . . . . . . . 87
Contents xv
15.1 Possible input types and the associated Shiny functions for pa-
rameterized reports. . . . . . . . . . . . . . . . . . . . . . . 76
xvii
List of Figures
xix
xx List of Figures
16.1 A graph generated using the sigma.js library and the sigma
package. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
The document format “R Markdown” was first introduced in the knitr pack-
age (Xie, 2015, 2018) in early 2012. The idea was to embed code chunks (of R
or other languages) in Markdown documents. In fact, knitr supported sev-
eral authoring languages from the beginning in addition to Markdown, in-
cluding 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.
The rmarkdown package (Allaire et al., 2018) 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:
xxiii
xxiv Preface
• Create notebooks in which you can directly run code chunks interactively.
Epictetus once said, “Wealth consists not in having great possessions, but in hav-
ing 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 oppo-
nents.
example, you may check out the PDF report2 of the 2017 Employer Health
Benefits Survey3 . 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 sur-
prisingly, 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 com-
piled to multiple types of output formats. Again, this is mainly due to the
simplified syntax of the authoring language, Markdown. The simpler the el-
ements in your document are, the more likely that the document can be con-
verted 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 repro-
ducible 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 automatically updated. You
can enjoy reproducibility and convenience at the same time.
• I know this is an R Markdown book, but I use Python more than R (Go
to Section 2.7.1).
• I know the output format I want to use, and I want to customize its ap-
pearance (Check out the documentation of the specific output format in
Chapter 3 or Chapter 4). For example, I want to customize the template
for my PowerPoint presentation (Go to Section 4.4.1).
• I heard about yolo = TRUE from a friend, and I’m curious what that means
in the xaringan package (Go to Chapter 7).
• I want to build a personal website (Go to Chapter 10), or write a book (Go
to Chapter 12).
If you are not familiar with R Markdown, we recommend that you read at
least Chapter 2 to learn the basics. All the rest of the chapters in this book
can be read in any order you desire. They are pretty much orthogonal to
each other. However, to become familiar with R Markdown output formats,
you may want to thumb through the HTML document format in Section 3.1,
because many other formats share the same options as this format.
Preface xxvii
Part III lists about ten R Markdown extensions that enable you to build dif-
ferent applications or generate output documents with different styles. Chap-
ter 5 introduces the basics of building flexible dashboards with the R pack-
age flexdashboard. Chapter 6 documents the tufte package, which provides
a unique document style used by Edward Tufte. Chapter 7 introduces the
xaringan package for another highly flexible and customizable HTML5 pre-
sentation format based on the JavaScript library remark.js. Chapter 8 docu-
ments the revealjs package, which provides yet another appealing HTML5
presentation format based on the JavaScript library reveal.js. Chapter 9 intro-
duces a few output formats created by the R community, such as the pretty-
doc package, which features lightweight HTML document formats. Chapter
10 teaches you how to build websites using either the blogdown package
or rmarkdown’s built-in site generator. Chapter 11 explains the basics of the
pkgdown package, which can be used to quickly build documentation web-
sites for R packages. Chapter 12 introduces how to write and publish books
with the bookdown package. Chapter 13 is an overview of the rticles package
for authoring journal articles. Chapter 14 introduces how to build interactive
tutorials with exercises and/or quiz questions.
Part IV covers other topics about R Markdown, and some of them are ad-
vanced (in particular, Chapter 16). Chapter 15 introduces how to generate dif-
ferent reports with the same R Markdown source document and different pa-
rameters. Chapter 16 teaches developers how to build their own HTML wid-
gets for interactive visualization and applications with JavaScript libraries.
Chapter 17 shows how to create custom R Markdown and Pandoc templates
so that you can fully customize the appearance and style of your output doc-
ument. Chapter 18 explains how to create your own output formats if the
existing formats do not meet your need. Chapter 19 shows how to combine
the Shiny framework with R Markdown, so that your readers can interact
xxviii Preface
with the reports by changing the values of certain input widgets and seeing
updated results immediately.
xfun::session_info(c(
'blogdown', 'bookdown', 'knitr', 'rmarkdown', 'htmltools',
'reticulate', 'rticles', 'flexdashboard', 'learnr', 'shiny',
'revealjs', 'pkgdown', 'tinytex', 'xaringan', 'tufte'
), dependencies = FALSE)
We do not add prompts (> and +) to R source code in this book, and we
comment out the text output with two hashes ## by default, as you can
see from the R session information above. This is for your convenience
when you want to copy and run the code (the text output will be ignored
since it is commented out). Package names are in bold text (e.g., rmark-
down), and inline code and filenames are formatted in a typewriter font
(e.g., knitr::knit('foo.Rmd')). Function names are followed by parenthe-
ses (e.g., blogdown::serve_site()). The double-colon operator :: means ac-
cessing an object from a package.
Acknowledgments
I started writing this book after I came back from the 2018 RStudio Confer-
ence in early February, and finished the first draft in early May. This may
sound fast for a 300-page book. The main reason I was able to finish it quickly
was that I worked full-time on this book for three months. My employer,
RStudio, has always respected my personal interests and allowed me to fo-
cus on projects that I choose by myself. More importantly, I have been taught
several lessons on how to become a professional software engineer since I
joined RStudio as a fresh PhD, although the initial journey turned out to be
painful.4 It is a great blessing for me to work in this company.
The other reason for my speed was that JJ and Garrett had already prepared
a lot of materials that I could adapt for this book. They had also been offering
suggestions as I worked on the manuscript. In addition, Michael Harper5 con-
tributed the initial drafts of Chapters 12, 13, 15, 17, and 18. I would definitely
not be able to finish this book so quickly without their help.
The most challenging thing to do when writing a book is to find large blocks
of uninterrupted time. This is just so hard. Both others and myself could in-
terrupt me. I do not consider my willpower to be strong: I read random ar-
ticles, click on the endless links on Wikipedia, look at random Twitter mes-
sages, watch people fight on meaningless topics online, reply to emails all
4
https://yihui.name/en/2018/02/career-crisis/
5
http://mikeyharper.uk
xxx Preface
the time as if I were able to reach “Inbox Zero”, and write random blog posts
from time to time. The two most important people in terms of helping keep
me on track are Tareef Kawaf (President of RStudio), to whom I report my
progress on the weekly basis, and Xu Qin6 , from whom I really learned7
the importance of making plans on a daily basis (although I still fail to do
so sometimes). For interruptions from other people, it is impossible to iso-
late myself from the outside world, so I’d like to thank those who did not
email me or ask me questions in the past few months and used public chan-
nels instead as I suggested8 . I also thank those who did not get mad at me
when my responses were extremely slow or even none. I appreciate all your
understanding and patience. Besides, several users have started helping me
answer GitHub and Stack Overflow questions related to R packages that I
maintain, which is even better! These users include Marcel Schilling9 , Xiany-
ing Tan10 , Christophe Dervieux11 , and Garrick Aden-Buie12 , just to name a
few. As someone who works from home, apparently I would not even have
ten minutes of uninterrupted time if I do not send the little ones to daycare,
so I want to thank all teachers at Small Miracles for freeing my daytime.
As I was working on the draft of this book, I received a lot of helpful re-
views from these reviewers: John Gillett (University of Wisconsin), Rose Hart-
man (UnderstandingData), Amelia McNamara (Smith College), Ariel Mul-
doon (Oregon State University), Yixuan Qiu (Purdue University), Benjamin
Soltoff (University of Chicago), David Whitney (University of Washington),
and Jon Katz (independent data analyst). Tareef Kawaf (RStudio) also volun-
teered to read the manuscript and provided many helpful comments. Aaron
Simumba16 , Peter Baumgartner17 , and Daijiang Li18 volunteered to carefully
correct many of my typos. In particular, Aaron has been such a big helper
with my writing (not limited to only this book) and sometimes I have to com-
pete with him19 in correcting my typos!
There are many colleagues at RStudio whom I want to thank for making it so
convenient and even enjoyable to author R Markdown documents, especially
the RStudio IDE team including J.J. Allaire, Kevin Ushey, Jonathan McPher-
son, and many others.
Lastly, I want to thank my editor, John Kimmel, for his continued help with
my fourth book. I think I have said enough about him and his team at Chap-
man & Hall in my previous books. The publishing experience has always
been so smooth. I just wonder if it would be possible someday that our metic-
15
https://github.com/jgm/pandoc
16
https://asimumba.rbind.io
17
http://peter.baumgartner.name
18
https://daijiang.name
19
https://github.com/rbind/yihui/commit/d8f39f7aa
20
https://twitter.com/kwbroman/status/922545181634768897
xxxii Preface
Yihui Xie
Elkhorn, Nebraska
About the Authors
This book is primarily put together by me (Yihui Xie), making use of the exist-
ing R documentation of the rmarkdown package and the rmarkdown web-
site, which were mainly contributed by J.J. Allaire and Garrett Grolemund.
Yihui Xie
Yihui Xie (https://yihui.name) is a software engineer at RStudio (https:
//www.rstudio.com). He earned his PhD from the Department of Statistics,
Iowa State University. He is interested in interactive statistical graphics and
statistical computing. As an active R user, he has authored several R pack-
ages, such as knitr, bookdown, blogdown, xaringan, tinytex, animation,
DT, tufte, formatR, fun, xfun, mime, highr, servr, and Rd2roxygen, among
which the animation package won the 2009 John M. Chambers Statistical
Software Award (ASA). He also co-authored a few other R packages, includ-
ing shiny, rmarkdown, and leaflet.
He has authored two books, Dynamic Documents with knitr (Xie, 2015), and
bookdown: Authoring Books and Technical Documents with R Markdown (Xie,
2016), and co-authored two book, blogdown: Creating Websites with R Mark-
down (Xie et al., 2017), and R Markdown: The Definitive Guide (Xie et al., 2018).
xxxiii
xxxiv About the Authors
J.J. Allaire
J.J. Allaire is the founder of RStudio and the creator of the RStudio IDE. J.J.
is an author of several packages in the R Markdown ecosystem including
rmarkdown, flexdashboard, learnr, and radix.
Garrett Grolemund
Garrett Grolemund is the co-author of R for Data Science and author of Hands-
On Programming with R. He wrote the lubridate R package and works for
RStudio as an advocate who trains engineers to do data science with R and
the Tidyverse. If you use R yourself, you may recognize Garrett from his
video courses on Datacamp.com and O’Reilly media, or for his series of pop-
ular R cheatsheets distributed by RStudio.
Garrett earned his PhD in Statistics from Rice University in 2012 under the
guidance of Hadley Wickham. Before that, he earned a Bachelor’s degree in
Psychology from Harvard University and briefly attended law school. Gar-
rett has been one of the foremost promoters of Shiny, R Markdown, and the
Tidyverse, documenting and explaining each in detail.
Part I
Get Started
0.0 3
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
1
Installation
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
5
2
Basics
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
7
8 2 Basics
2.1.7 Books
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
2.1.8 Websites
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
FIGURE 2.4: The output formats listed in the dropdown menu on the RStu-
dio toolbar.
2.6.1 Figures
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
2.6.2 Tables
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
2.7.1 Python
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
2.7.3 SQL
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
2.7.4 Rcpp
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
2.7.5 Stan
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
2.7.7 Julia
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
Output Formats
2.8 15
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
3
Documents
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
17
18 3 Documents
TABLE 3.1: The possible values of the df_print option for the
html_document format.
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
3.1.10.2 Includes
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
3.2 Notebook
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
3.2 Notebook 21
FIGURE 3.6: Insert a new chunk from the code selected before.
FIGURE 3.7: The indicator in the gutter to show the execution progress of a
code chunk in the notebook.
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
TABLE 3.3: The possible values of the df_print option for the pdf_document
format.
TABLE 3.4: Available top-level YAML metadata variables for LaTeX output.
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
3.3.7.3 Includes
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
24 3 Documents
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
27
28 4 Presentations
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
4.1.6 Tables
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
4.3.1 Themes
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
FIGURE 4.4: Two sample slides with the AnnArbor theme in Beamer.
Extensions
4.4 35
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
5
Dashboards
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
5.1 Layout
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
37
38 5 Dashboards
5.2 Components
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
5.2.2 Gauges
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
5.3 Shiny
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
6.1 Headings
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
6.2 Figures
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
41
42 6 Tufte Handouts
6.3 Sidenotes
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
6.4 References
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
6.5 Tables
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
6.7 Responsiveness
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
FIGURE 7.1: Two sample slides created from the xaringan package.
45
46 7 xaringan Presentations
FIGURE 7.2: Separate the current display from the external display.
7.6.5 Macros
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
7.6.6 Disadvantages
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
8
reveal.js Presentations
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
FIGURE 8.1: Two sample slides created from the revealjs package.
49
50 8 reveal.js Presentations
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
9.1.1 Usage
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
53
54 9 Community Formats
FIGURE 9.3: A few sample slides created via the Shower presentation frame-
work.
10
Websites
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
10.3 Deployment
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
55
56 10 Websites
10.5.2.1 RStudio
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
10.5 rmarkdown’s site generator 57
10.5.3.1 Content
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
10.5.3.2 R scripts
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
10.5.9.2 Examples
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
11
HTML Documentation for R Packages
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
11.2 Components
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
59
60 11 HTML Documentation for R Packages
11.2.3 Articles
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
11.2.4 News
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
61
62 12 Books
12.2.3 _bookdown.yml
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
12.2.4 _output.yml
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
12.4.1 HTML
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
12.4.2 LaTeX/PDF
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
12.4.3 E-books
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
12.5 Editing
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
FIGURE 12.2: The Build tab within RStudio highlighting bookdown output
formats.
12.6 Publishing
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
12.6.3 Publishers
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
13
Journals
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
FIGURE 13.1: Two journal templates in the rticles package (PLOS and
Springer).
65
66 13 Journals
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
14.3 Exercises
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
67
68 14 Interactive Tutorials
14.3.1 Solutions
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
14.3.2 Hints
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
14.5 Videos
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
Other Topics
14.7 73
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
15
Parameterized reports
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
75
76 15 Parameterized reports
TABLE 15.1: Possible input types and the associated Shiny functions for pa-
rameterized reports.
15.4 Publishing
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
16
HTML Widgets
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
16.1 Overview
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
FIGURE 16.1: A graph generated using the sigma.js library and the sigma
package.
16.2.2 Dependencies
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
77
78 16 HTML Widgets
16.2.3 R binding
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
16.2.5 Demo
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
16.3.1 Requirements
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
16.3.2 Scaffolding
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
16.5.1.1 HTMLWidgets.dataframeToD3()
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
16.5.1.2 HTMLWidgets.transposeArray2D()
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
80 16 HTML Widgets
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
81
82 17 Document Templates
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
83
19
Shiny Documents
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
FIGURE 19.2: Increase the number of rows in the table in a Shiny document.
19.2 Deployment
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
19.2.1 ShinyApps.io
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
85
86 19 Shiny Documents
19.7.1 A caveat
This PDF is only a skeleton. Please either read the free online HTML version,
or purchase a hard-copy of this book.
Bibliography
Allaire, J., Xie, Y., McPherson, J., Luraschi, J., Ushey, K., Atkins, A., Wickham,
H., Cheng, J., Chang, W., and Iannone, R. (2018). rmarkdown: Dynamic Doc-
uments for R. R package version 1.11.
Xie, Y. (2015). Dynamic Documents with R and knitr. Chapman and Hall/CRC,
Boca Raton, Florida, 2nd edition. ISBN 978-1498716963.
Xie, Y., Allaire, J., and Grolemund, G. (2018). R Markdown: The Definitive Guide.
Chapman and Hall/CRC, Boca Raton, Florida. ISBN 9781138359338.
Xie, Y., Hill, A. P., and Thomas, A. (2017). blogdown: Creating Websites with
R Markdown. Chapman and Hall/CRC, Boca Raton, Florida. ISBN 978-
0815363729.
89