R, knitr & markdown = HTML

Welcome to this demo of how R code and results can be combined into an HTML report. This entire blogpost was generated by using a combination of R, knitr and markdown.

Beforehand, make sure you have the following libraries installed (latest version);

  • knitr
  • markdown
  • ggplot2 (to run the example script)

Syntax references for markdown can be found here.

Let's make two separate files, a .R file and an .Rmd file.

The .R file

The .R file should be used to load necessary libraries, define and transform data and, possibly, to define the plots beforehand. After a useful comment from Yihui I recommend including the code to define the dataset and create the plot within the .Rmd file. This in order to create a self-contained document that can be reproduced without special instructions. As, and I quote from the comment below: 'a reproducible report should be self-contained and not rely on external objects'.

My .R file looks as follows:

library(knitr)

#transform the .Rmd to a markdown (.md) file and convert it to html
knit2html('r-knitr-markdown.Rmd',fragment.only=TRUE)

When using the output in a blogpost it is important to set the 'fragment.only' option to TRUE, otherwise it will generate a full-fledged HTML page (HTML tags / CSS / etc.).

In the .Rmd file, the following code is used to start a so called R 'chunk'. A 'chunk' is a place in the .Rmd file where R code will be run and evaluated. An R chunk is defined as follows;

R chunk

Let's try this and check out the result.

# your R code goes here, e.g.
1 + 1
## [1] 2

Let's create a dataset and define a plot for testing purposes.

# define the dataset
dataset <- data.frame(x = seq(1, 100), y = runif(100))

# let's load the ggplot2 library
library(ggplot2)

# define the plot to be used in the final HTML report
graph <- ggplot(dataset, aes(x = x, y = y)) + geom_line(colour = "red")

What if we print the plot thas was defined in the 'graph' variable? This would be the result;

# show the graph
graph

plot of chunk unnamed-chunk-3

There are a bunch of options available to e.g. hide the printout of the R script (echo) or to hide warning messages. Also the size of the graph output can be adjusted. All these option are explained on Yihui's knitr website.

I hope this gives an idea of how R can be used to generate reproducible data analysis reports, easy-to-update business intelligence reports and of course blog posts.

The files used to produce this output can be found here;

7 thoughts on “R, knitr & markdown = HTML

  1. Thanks for the example, but I strongly recommend you to move the code to create ‘graph’ in the R script into the Rmd file so that the Rmd document is self-contained, otherwise it is not reproducible without special instructions (that is bad). Ideally a reproducible report should be self-contained and not rely on external objects.

    BTW, you do not need print(graph); just graph is fine. And knit2html() is a wrapper to knit()+markdownToHTML(), so knit2html(‘r-knitr-markdown.Rmd’) is enough.

  2. Pingback: glass spigot