R, knitr & markdown = HTML

July 7, 2012
By

(This article was first published on FishyOperationsCategory Archives: r, and kindly contributed to R-bloggers)

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.

• 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)
library(markdown)

#transform the .Rmd to a markdown (.md) file.
knit('r-knitr-markdown.Rmd')

#transform the .md to HTML format
markdownToHTML("r-knitr-markdown.md", "r-knitr-markdown.html",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;

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


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;

The post R, knitr & markdown = HTML appeared first on FishyOperations.