NB R Markdown

Programming for Data Science

Literate Programming

R provides a notebook style coding environment similar to Jupyter called R Markdown.

It follows the literate programming paradigm of inter-leaving text and runnable code.

As with Jupyter, they can be used interactively or as exported documents into standard formats.

R Markdown files consists of the three types of element:

  1. Metadata, written in YAML (at the top of the file).
  2. Text, written in Markdown.
  3. Code, written in one of several supported programming languages.

All of these are plain text and live in the same .Rmd file.

Markdown

As the name implies, R Markdown files uses a version of Markdown to create the text components and define the code blocks.

Mardown is a simplified version of HTML markup that uses typographic symbols and line spaces instead of angle brackets.

For example, instead of writing this in HTML:

This is is header

This paragraph defines a link/

You would write this:

This is a header

This paragraph defines a link.

The markdown gets converted into HTML by a viewer or converter at display time.

Code

The code blocks are delimmited by three backtacks ``` with a language specified in braces.

For example, to run a block of R code, you would enter this:

x <- 10
print(x)

R Markdown also supports running code in other languages in the same document.

To run a Python block, you could do something like this:

x = 10
print(x)

In RStudio, these blocks of code can be executed in place, with results output in below the cell, as with Jupyter.

Comparison to Jupyter

R Markdown files are optimized to be published documents.

Jupyter notebooks are meant primarily to be interactive environments.

R Markdown files are plain text.

Jupyter notebooks are written in JavaScript.

JavaScript is the native programming language of web browsers; the notebook format is executable code that can be integrated into a web application.

Varieties of R Markdown outputs

R Markdown is designed to be output to several formats, including notebooks, documents, presentations, and interactive applications.

Notebooks and documents can be output as HTML, PDF, or Word files.

Presentations may be output in IOSlides, Beamer, or PowerPoint format.

Interactive applications are interactive apps that can be published on the web with a framework called Shiny.

Knitting

Knitting is the process of compiling all the code and visual assets of an R Markdown into a single file.

Publishing

RMarkdown has publication in mind as an outcome. RStudio allows you to publish in these destinations:

Outputting

There are two ways to set the output of a document:

  1. Permanently, by modifying the YAML header:
---
title: "Viridis Demo"
output: html_document
---
  1. Transiently, by calling rmarkdown::render() by hand:

rmarkdown::render(“diamond-sizes.Rmd”, output_format = “word_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.

Output options

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: true
    toc_float: true
---

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

---
output:
  html_document:
    toc: true
    toc_float: true
  pdf_document: default
---

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

How It Works