Automated PDF reports using R Markdown

September 28, 2015 ·
Jason Hwa

Many of the graphics that we have produced on this blog as well as in collateral for Orchard are built using an open-source statistical software called R. For those who are not familiar with R, it is a simple, dynamic programming language that can be used to generate completely customizable plots that can be updated easily. It has a strong community following, and many data scientists contribute packages that others may find useful, for free! One capability that we have found to be a great tool is the automation of reports using R Markdown.

The R Markdown language is an extension of LaTeX which allows for directly embedding R output into documents such as PDF, HTML, or Word document. For example, we’ve been generating images in R and including them in our blog posts like such:

With a short wrapper in R Markdown, we can embed the graph in a pdf with the following code:

To keep the markdown file clean, we store the R code that generates the plot in a separate file. R is extremely flexible and allows for customization of everything from date format to axis line style to colors of the bars. In the above chart, we aggregate origination amounts by date to show the trend over time but the stacked bar chart is just one simple example of many different graphics that R can generate.

In R, we commonly work with data tables which allow for quick matrix calculations. There is an excellent R package called pander which is useful for outputting nicely formatted tables into the documents. Pass the table object to the pander function like so:

Putting it all together, after adding a few more plots and stylizing the header and footer, we can use the R Markdown facility to generate a market data report. When we receive new data from originators, we just need to click a button and the code takes care of updating all of the plots and tables automatically! The result is a nicely formatted PDF report that you can download here: Consumer Unsecured Quarterly Profile.

There are a few limitations, and LaTeX is a bit difficult to work with, but the R Markdown facility is by far the best PDF report builder that we have found so far. Any graphic (non-interactive) that can be built in R can be incorporated into the document, and the possibilities are limitless. For more ideas on the types of graphics that R can produce, check out a gallery here: R Gallery.