Creating dynamic reports using R Markdown

R Markdown is a formatting syntax that makes it easy to create dynamic reports. It’s based on the popular Markdown lightweight markup language for web writing.

Dynamic Reporting


Markdown is a response to the growing importance of writing for on-line publication. Tools based around the concept of a printed page (e.g. Word, Google Docs) make little sense when writing for the web.

However, HTML, the native markup language of the web is a little ungainly for writers. Even for those of us who are comfortable with HTML, the syntax gets in the way of writing. Markdown uses a much friendlier lightweight syntax that is readable in its raw form, but can trivially be transformed to HTML.

For example, instead of writing:


<p>Markdown is <em>much</em> easier to use than HTML because</p>

  <li>it doesn't clutter the page</li>
  <li>it has an intuitive syntax

we would write

# Introduction

Markdown is *much* easier to use than HTML because

- it doesn't clutter the page
- it has an intuitive syntax

I know which version I’d rather use when writing.

R Markdown

R Markdown extends the basic Markdown syntax to allow executable R code to be included in the document. The output of the R code, including charts, can be rendered as part of the document.

Combining Markdown and code to create live reports is becoming a popular approach in data science—both for reporting and education. Similar capabilities are provided by:

Creating an R Markdown dynamic report

The popular R IDE RStudio supports the creation of R Markdown documents.

Create a new R Markdown document using

File > New File > R Markdown...

The default options are fine.

Delete the current contents and save the empty file.

Let’s do some simple reporting. We’ll use the 2012 General Social Survey dataset from the tigerstats package. Add the following Markdown text

# 2012 General Social Survey analysis

This report contains a basic exploratory analysis of the 2012 General Social Survey 
data from the `tigerstats` package.

We can preview the current document using the Knit HTML button on the code editor toolbar.

Knit HTML toolbar

To embed live code in the document we need to insert an R code chunk. We can use Ctrl+Alt+I to do this. This will insert the following text



The {r} indicates that this is an R language code chunk. Edit the chunk as follows

```{r include=FALSE}


include=FALSE tells R Markdown to execute the chunk, but hide any evidence of it from the reader.

Add the following

Survey response count by gender is

```{r echo=FALSE, comment=NA}


comment=NA removes # symbols from the output.

Knit the document (press the Knit HTML button) to see the results.

 886   1088 

Now add the following

Let's see how political persuasion correlates with views on capital punishment.

```{r echo=FALSE, comment=NA}
prop.table(table(gss2012$polviews, gss2012$cappun))


The table suggests that, as a person's political stances becomes more conservative 
that person tends to be more likely to favour capital punishment. No news there. 
What might be more surprising that people have to hold quite strong liberal views 
before they have a tendency to oppose capital punishment.

Again, Knit the document and see how our report is developing.

We’ve only just started to scratch the surface of R Markdown’s capabilities, but I hope this has given you an idea of what it does and whetted your appetite for how you could use it in your own data analysis work. RStudio’s excellent R Markdown cheat sheet provides a concise overview of most of the features and how you use them.

The complete code for this article is available as a Gist.

[Disclaimer: The analysis performed above was only to illustrate the use of R Markdown. No conclusions should be drawn from it.]

If you are interested in learning more about R, Learning Tree provides the following courses

Type to search

Do you mean "" ?

Sorry, no results were found for your query.

Please check your spelling and try your search again.