Literate programming environments try to minimise the potential for undocumented/write-only code by providing a simple method of combining text, images and other output to completely explain the function of the code alongside the code.
RStudio provides an amazing literate programming environment in the form of RMarkdown. A simple document-orientated approach to coding that allows even htmlwidgets to be included directly alongside code.
There are three different kinds of document that can be created with RMarkdown:
All of the materials for this training course were written in RMarkdown.
Markdown is a very simple and widely used markup language created by John Gruber - it allows documents to be described and then generated through an interpreter.
There are many different flavours of Markdown but they all share the same basic syntax, which can be spit into the following types of actions:
Laying out documents
Formatting Text
Including images and hyperlinks
Include code and code output
Every flavour of Markdown has slightly different implementations, before we get into the specifics of “RMarkdown” let’s create a document that allows us to generate output from RMarkdown.
This course focuses on presentations rather than reports, as I think it’s quite exciting to be able to include interactive content within data-driven presentations.
There are three types of presentations you can create:
No matter which document type you choose, you’ll find yourself with a template documet that looks very much like this:
When writing your presentations, you will want to run your code to ensure that it works. By default, RMarkdown documents behave very differently from R script files:
Basic text formatting is achieved using syntax that you may have used in other text editors, or in comment sections on websites:
*italics*
**bold**
Bullet point and enumerated lists are also added easily as follows:
- Bullet point 1
- Bullet point 2
First Numbered Item
Second Numbered Item
It’s important to remember that as your text is first interpreted as code, rather than text to display, it’s necessary to “escape” characters that would otherwise be interpreted - for instance \*this isn’t italic\*
URL links are simply written as
[text to show](http://google.com)
Images can be embedded into RMarkdown documents from the web or from your local machine - the exercises will introduce you to the relative filepaths necessary to include local files.

Markdown is great for quickly specifying your document/presentation layout, but it is not designed to be flexible or extensible - it’s a minimal set of instructions for styling your content.
If you’re generating HTML from RMarkdown it’s possible to simply write HTML directly in your .rmd files.
Slidy and ioslides both utilise the Bootstrap CSS framework so it’s easy to include responsive content in your presentations. For instance you may have noticed this earlier in the course, which uses the [awesome] Bootstrap grid framework, which you can read about here: https://v4-alpha.getbootstrap.com/layout/grid/
Charts from scratch
highcharthc_add_serieshc_add_series are consistent with the Highchart JS library’s requirements.Charts with magic
hchart functionhcaeshtmlwidgets can fairly easily be embedded into RMarkdown documents, however there are some things to be wary of.
For instance, the zoom buttons in leaflet maps unfortunately result in a slidy presentation changing slides:
library("leaflet")
quakes %>%
leaflet() %>%
addTiles() %>%
addCircleMarkers()Sometimes there are simple modifications you can make to htmlwidgets to make them play nicely with your slides, like dropping the zoom controls:
quakes %>%
leaflet(options = leafletOptions(zoomControl = FALSE)) %>%
addTiles() %>%
addCircleMarkers()There’s a package called widgetframe which is designed to make it easier to embed htmlwidgets into RMarkdown documents (and other places) without conflicts between the presentation and the iframe which contains the widget itself.
library("leaflet")
library("widgetframe")
l <- quakes %>%
leaflet() %>%
addTiles() %>%
addCircleMarkers()
frameWidget(l)RPubs.com is a free and open platform for publishing and sharing HTML RMarkdown documents/presentations with others.
Its’s incredibly simple to publish to RPubs - simply click on the publish button:
Note that while documents hosted on RPubs are made public, the underlying code/.rmd files cannot be downloaded by others.