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);
- ggplot2 (to run the example script)
Syntax references for markdown can be found here.
The .R file
The .R file should be used to load necessary libraries,
define and transform data and, possibly, to define the plots beforehand.
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 ##  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;