R Reporting Part 3: Using Rhtml for Batch Web Reporting

This is the third of a series of article on how to use R, RStudio and TexMaker to prepare presentations and batch jobs for automated reporting on a web server or Microsoft SharePoint server. The series is based upon the presentation that I did at the February 27, 2016 Dallas R User Group Meetup. Because the presentation was primarily a demonstration, there really isn’t a presentation to distribute; this series covers the topics from the presentation/demonstration. The series will eventually include the following articles as I complete them over the next couple of weeks:

The series of articles describes the process for daily batch jobs that generate the Daily Econometric Graphs web page which includes links to the same econometric charts in several formats, all generated through the same R code:

All of the examples are based upon the knitr R package; you should reference the knitr documentation, as this article is not a replacement for the knitr documentation.

Example source is available in images/documents/econometric_source.zip.

Using Rhtml for Batch Web Reporting

The first step in creating the batch report is to create a .Rhtml file in RStudio as shown in Figure 1:

Figure 1. Create the .Rhtml file in RStudio.
Creating an Rhtml file in RStudio

Call all Needed Libraries in the prologue knitr Chunk

Next, strip out (or reuse) the default HTML with the tags for the "prologue" in the file where all of the required R libraries are included:

<!--begin.rcode prologue,fig.path="images/figures/",fig.width=7, fig.height=5,fig.dpi=100,message=FALSE,warning=FALSE,echo=FALSE setwd("~/svn_work/Consulting_Business/web_site/articles/econometric_charts/src/R") library(ggplot2) library(fImport) library(reshape2) library(scales) options('download.file.method'='wget') read_chunk("econometric_charts_chunks.R") #R.Version() end.rcode-->

All of this should be familiar except for the lines

options('download.file.method'='wget') read_chunk("econometric_charts_chunks.R")

The options line changes the download method used by the Rcurl package (a dependency loaded by fImport) to one that works on my Linux machine, as the default download method does not work on my machine. The read_chunk line is the one that is important for batch reporting.

When you create an R report that will be compiled by knitr, you can either put the code inline, or create it in a separate .R file that can be called from multiple .Rhtml or .Rnw files; knitr reads these chunks from the file specified in the read_chunk("filename.R") call at the beginning of your .Rhtml or .Rnw file. From a re-use perspective, the chunk method is very helpful.

“prologue” is not a reserved word nor does knitr require this label; this is just a personal standard for all of the .Rhtml and .Rnw files that I create.

Create a Figure and call the R Chunk to Generate the Graphic

The next step is to add the <figure> and related tags, with the knitr markup to call our code chunk from the econometric_charts_chunks.R file:

<figure> <figurecap>Cost of Funds Benchmark–One through five year swap Rates</figurecap> <!--begin.rcode costOfFunds,fig.path="images/figures/",fig.width=7, fig.height=5,fig.dpi=100,message=FALSE,warning=FALSE,echo=FALSE,cache=FALSE </figure>

All R code in the .Rhtml file must be enclosed with the knitr tags:




From and HTML perspective, this is a comment, but knitr looks for these special comment delimiters and run any R code that is enclosed. Most of the knitr options in this example are self explanatory, but the fig.path option requires some discussion. By default, knitr will create a figure directory in whatever the root directory is–your home directory unless you otherwise set it, as was done in the prologue section in this example. If you plan to upload this to a web server, you will need to create an image storage directory structure that matches the image directory structure on your web server. On my web server, the image directory for most articles is images/figures. If you do not set the fig.path, you will need to manually correct the .html file that knitr generates each time you upload everything.

Writing the Knitr Chunk File econometric_charts_chunks.R

The last step is to add the R code to the generate the graphic to the econometric_charts_chunks.R so that knitr will generate the graphic. In the .R file, the costOfFunds

<!--begin.rcode costOfFunds

label for the chuck must match the chunk label ## ---- costOfFunds ---- in the econometric_charts_chunks.R file:

# ## ---- costOfFunds ---- # # Make the figure with swap rates # #swpTs<-fredSeries("MSWP1") swpTs<-fredSeries(c("MSWP5","MSWP3","MSWP2","MSWP1"),nDaysBack=365*10,to=Sys.timeDate()) swpTs$dateVal = as.Date(rownames(swpTs)) swpDf <- data.frame(swpTs) swpDf$dateVal <- as.character(swpDf$dateVal) # melt doesn't work with Date data types #summary(swpDf) swpDf <- melt(swpDf) swpDf$dateVal <- as.Date(swpDf$dateVal) # ggplot's scale_x_date doesn't work with character data types colnames(swpDf)[2] <- "Series" swpIntPlot <- ggplot(data=swpDf,aes(swpDf$dateVal,swpDf$value,group=swpDf$Series)) + geom_line() #swpIntPlot + ggtitle("Swap Rates") + xlab("Date") + ylab("Rate") + scale_x_date() swpIntPlot + ggtitle("Swap Rates") + xlab("Date") + ylab("Rate")+ scale_x_date() + geom_point(aes(color=Series)) + theme(panel.background = element_rect(colour="#f4f6f7"))

The remaining figures are added with similarly.

Adding the Epilog

The final chunk in the report file is probably a good practice or perhaps a time saving measure; an epilog to run image compression on any image files that were created by the script:

<!--begin.rcode epilog,echo=FALSE,message=FALSE,warning=FALSE,cache=FALSE system('optipng("images/figures/*.png")') end.rcode-->

The optipng utility compresses .png files and will improve the load time for your page. If you choose to generate .jpg or other file types, you can call other utilities to compress them.

Running knitr to Generate the HTML File

The final step is to run knitr to process the file; to do this, click on the “Knit” icon at the top of the active tab in RStudio, as shown in Figure 2:

Figure 2. Running knitr to generate the HTML file and all figures.
Running knitr in RStudio to generate an HTML file from the Rhtml source file