Making your R Markdown Pretty

R Markdown documents can be customized to your liking and can be made way prettier than the defaults.

There are number of different themes that you can use out of the box! You can do this by changing your YAML header. The YAML header is the section at the top of your document enclosed by three --- on the top and the bottom. You can provide additional metadata to the header in the format of parameter: value. To change the default theme of the html_document output format we can adjust the header to look like the below.

---
title: "Super creative title"
author: "Your name"
date: The day of course.
output:
  html_document:
    theme: united
---

Notice that we’re adding some new lines and indentation to the output parameter. Think of this similarly how you use bulleted list indentation. The indentation is related to the thing above it. In the above example we’re telling output to be an html_document, then further under the html document, we’re specifying a theme.

Bootswatch themes

The themes that R Markdown can use out of the box are called Bootswatch Themes. These are some pre-created themes from web designers that can be applied to our document. The values you can pass to the theme parameter are default, cerulean, journal, flatly, darkly, readable, spacelab, united, cosmo, lumen, paper, sandstone, simplex, yeti

Creating a table of contents

Similar to how we can modify the theme of a document, we can add a table of contents to help the reader navigate through the document. Under html_document: we can put the value toc: true to create a clickable table of contents at the beginning of the document. This is super helpful for people who want to navigate through your document or figure out where they’d like to start.

Your YAML would look like

output:
  html_document:
    theme: united
    toc: true

Floating table of contents

To be an even fancier R Markdown creator, you can modify your table of contents to be floating. That means while a viewer is reading through your document the table of contents is still there and available to be clicked on and navigated. The modification is rather simple!

output:
  html_document:
    theme: united
    toc: true
    toc_float: true

Other styles

There are a ton of other packages out there that you can use to modify your R Markdown themes to be really pretty in other ways. A few of my favorites are: distill, tufte, and pagedown.

Try some of the following:

output: distill::distill_article. This requires the package distill.

output: tufte::tufte_html. This requires the packge tufte.

This requires the package cleanrmd. The instructions can be found at https://github.com/gadenbuie/cleanrmd.

output: 
  cleanrmd::html_document_clean:
    theme: awsm.css

This requires the package prettydoc.

output:
  prettydoc::html_pretty:
    theme: leonids

Note that you likely will need to install the related packages. You can do this with install.packages("distill"), etc.