Blog: R Markdown (faster, easier, reproducible research papers)

Consider your workflow for writing a paper or creating a presentation based on your research. Most researchers use one or several tools to conduct the analysis and produce charts, and then create a document in a separate tool, copying over tables and figures by hand.

There are major downsides to this workflow. Every time new data comes in or you make an adjustment to analysis, you have to re-create tables and figures and update values in the text. This process is time-consuming and error-prone. There is also often no record of what analysis pipeline produced each table or figure. If you return to the paper years later or share it with a researcher trying to reproduce your work, it can be very challenging to know how each figure was created.

R Markdown provides an alternative approach that lets you mix prose and code in one document, and automatically generate tables and figures each time you compile your document. No more typos as you copy over cells in a table, no more tedious manual rerunning of analyses and figure generation, and no more losing track of the code behind your paper. The outputs can be almost any format you can think of: PDFs, Word documents, HTML websites, slideshows, books, and even interactive dashboards.

For prose sections of your documents, R Markdown uses Markdown for formatting. Markdown is a lightweight system of annotations that let the system know how to format your text. Markdown supports headers, inline equations, images, block quotes, unordered and ordered lists, and tables, as well as a variety of global formatting options. Here's an example of some Markdown:

# Top-level heading

Regular text. *italic text*. **bold text**.

* This
* Is
* A
* List

 

And the output as HTML:

Top-level heading

Regular text. italic text. bold text.

  • This
  • Is
  • A
  • List

To include tables or figures in the document, you can interleave the code that produces the figure, and R Markdown will include it in the output. You can choose to include the code and outputs, just the code, or just the outputs. For example, the following code will insert a scatterplot of miles per gallon versus weight from the mtcars dataset:

```{r echo=FALSE}
library(ggplot2)
ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()
```

Don’t know or use R? No problem! R Markdown supports a wide variety of programming languages. You can include code chunks with languages like Python, Julia, Octave, SQL, C, and Fortran. A full list of supported languages is available here.

If you would like to learn more about using R Markdown in your research workflow, ACER is offering a 90-minute tutorial this fall. You can find details and registration here. You can also view the R Markdown website for an introduction, or check out this free online book (itself created using R Markdown!)