29 R Markdown formats

29.1 Introduction

So far you’ve seen R Markdown used to produce HTML documents. This chapter gives a brief overview of some of the many other types of output you can produce with R Markdown. There are two ways to set the output of a document:

This is useful if you want to programmatically produce multiple types of
output.

RStudio’s knit button renders a file to the first format listed in its output field. You can render to additional formats by clicking the dropdown menu beside the knit button.

29.2 Output options

Each output format is associated with an R function. You can either write foo or pkg::foo. If you omit pkg, the default is assumed to be rmarkdown. It’s important to know the name of the function that makes the output because that’s where you get help. For example, to figure out what parameters you can set with html_document, look at ?rmarkdown::html_document.

To override the default parameter values, you need to use an expanded output field. For example, if you wanted to render an html_document with a floating table of contents, you’d use:

output:html_document:toc: truetoc_float: true

You can even render to multiple outputs by supplying a list of formats:

output:html_document:toc: truetoc_float: truepdf_document: default

Note the special syntax if you don’t want to override any of the default options.

29.3 Documents

The previous chapter focused on the default html_document output. There are a number of basic variations on that theme, generating different types of documents:

pdf_document makes a PDF with LaTeX (an open source document layout
system), which you’ll need to install. RStudio will prompt you if you
don’t already have it.

word_document for Microsoft Word documents (.docx).

odt_document for OpenDocument Text documents (.odt).

rtf_document for Rich Text Format (.rtf) documents.

md_document for a Markdown document. This isn’t typically useful by
itself, but you might use it if, for example, your corporate CMS or
lab wiki uses markdown.

github_document: this is a tailored version of md_document
designed for sharing on GitHub.

Remember, when generating a document to share with decision makers, you can turn off the default display of code by setting global options in the setup chunk:

knitr::opts_chunk$set(echo =FALSE)

For html_documents another option is to make the code chunks hidden by default, but visible with a click:

output:html_document:code_folding: hide

29.4 Notebooks

A notebook, html_notebook, is a variation on a html_document. The rendered outputs are very similar, but the purpose is different. A html_document is focused on communicating with decision makers, while a notebook is focused on collaborating with other data scientists. These different purposes lead to using the HTML output in different ways. Both HTML outputs will contain the fully rendered output, but the notebook also contains the full source code. That means you can use the .nb.html generated by the notebook in two ways:

You can view it in a web browser, and see the rendered output. Unlike
html_document, this rendering always includes an embedded copy of
the source code that generated it.

You can edit it in RStudio. When you open an .nb.html file, RStudio will
automatically recreate the .Rmd file that generated it. In the future, you
will also be able to include supporting files (e.g. .csv data files), which
will be automatically extracted when needed.

Emailing .nb.html files is a simple way to share analyses with your colleagues. But things will get painful as soon as they want to make changes. If this starts to happen, it’s a good time to learn Git and GitHub. Learning Git and GitHub is definitely painful at first, but the collaboration payoff is huge. As mentioned earlier, Git and GitHub are outside the scope of the book, but there’s one tip that’s useful if you’re already using them: use both html_notebook and github_document outputs:

output:html_notebook: defaultgithub_document: default

html_notebook gives you a local preview, and a file that you can share via email. github_document creates a minimal md file that you can check into git. You can easily see how the results of your analysis (not just the code) change over time, and GitHub will render it for you nicely online.

29.5 Presentations

You can also use R Markdown to produce presentations. You get less visual control than with a tool like Keynote or PowerPoint, but automatically inserting the results of your R code into a presentation can save a huge amount of time. Presentations work by dividing your content into slides, with a new slide beginning at each first (#) or second (##) level header. You can also insert a horizontal rule (***) to create a new slide without a header.

29.6 Dashboards

Dashboards are a useful way to communicate large amounts of information visually and quickly. Flexdashboard makes it particularly easy to create dashboards using R Markdown and a convention for how the headers affect the layout:

29.7 Interactivity

29.7.1 htmlwidgets

HTML is an interactive format, and you can take advantage of that interactivity with htmlwidgets, R functions that produce interactive HTML visualisations. For example, take the leaflet map below. If you’re viewing this page on the web, you can drag the map around, zoom in and out, etc. You obviously can’t do that in a book, so rmarkdown automatically inserts a static screenshot for you.

29.7.2 Shiny

htmlwidgets provide client-side interactivity — all the interactivity happens in the browser, independently of R. On one hand, that’s great because you can distribute the HTML file without any connection to R. However, that fundamentally limits what you can do to things that have been implemented in HTML and JavaScript. An alternative approach is to use shiny, a package that allows you to create interactivity using R code, not JavaScript.

To call Shiny code from an R Markdown document, add runtime: shiny to the header:

title:"Shiny Web App"output: html_documentruntime: shiny

Then you can use the “input” functions to add interactive components to the document:

You can then refer to the values with input$name and input$age, and the code that uses them will be automatically re-run whenever they change.

I can’t show you a live shiny app here because shiny interactions occur on the server-side. This means that you can write interactive apps without knowing JavaScript, but you need a server to run them on. This introduces a logistical issue: Shiny apps need a Shiny server to be run online. When you run shiny apps on your own computer, shiny automatically sets up a shiny server for you, but you need a public facing shiny server if you want to publish this sort of interactivity online. That’s the fundamental trade-off of shiny: you can do anything in a shiny document that you can do in R, but it requires someone to be running R.

Execute rmarkdown::render_site() to build _site, a directory of files ready to deploy as a standalone static website, or if you use an RStudio Project for your website directory. RStudio will add a Build tab to the IDE that you can use to build and preview your site.

29.10 Learning more

To learn more about effective communication in these different formats I recommend the following resources:

To improve your presentation skills, I recommend
Presentation Patterns, by Neal Ford,
Matthew McCollough, and Nathaniel Schutta. It provides a set of effective
patterns (both low- and high-level) that you can apply to improve your
presentations.