R Reporting Part 6: Using LaTeX for PDF Articles

This is the sixth of a series of articles 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 Rnw for LaTeX/Beamer Presentations

LaTeX’s For most true automated reporting requirements, LaTex offers some very useful capabilities:

  • Table of Contents
  • List of Figures
  • Footnotes, in-line citations and automated bibliography for data references
  • Indexing

To create a LaTeX article, create an .Rnw file as shown in Figure 1. RStudio will populate the file with the basic LaTeX tags as shown in Figure 2, but for a more realistic document, you can probably start with something like the tags in Figure 3.

Figure 1. Create the .Rnw file in RStudio.
Creating an Rhtml file in RStudio
Figure 2. Default LaTeX stub in RStudio.
\documentclass{article} \begin{document} \end{document}
Figure 3. Basic LaTeX article with title page, Table of Contents and
\documentclass{article} %% Print version for presentation % % Printing options % \usepackage[]{tikz} \usepackage{hyperref} \usepackage{hyperxmp} \usepackage{multimedia} % allows video? \usepackage{ifplatform} \usepackage{verbatim} \usepackage{endnotes} %\let\enotesize=\tiny %\let\footnote=\endnote % % Determine the OS and set the path prefix % Note that \iflinux always retURs false unless shell escape is enabled % \ifwindows \newcommand{\mssPathDef}{C:/Users/bwmoore/Documents} \else \newcommand{\mssPathDef}{/home/bwmoore} \fi %\iflinux %\fi % % \hypersetup{ pdfcopyright={Copyright \copyright \the\year by Moore Software Services, LLC. All rights reserved.} pdfsubject={} } % % % \begin{document} \begin{titlepage} \begin{center} \begin{huge} \textbf{Selected Econometrics for \today} \\ \end{huge} Bruce Moore, DEng \\ Moore Software Services, LLC \\ P.O. Box 183\\ Coppell, TX 75019\\ (972) 652-0254\\ \texttt{This email address is being protected from spambots. You need JavaScript enabled to view it.} \\ \href{https://www.mooresoftwareservices.com}{www.mooresoftwareservices.com} \\ %\includegraphics[height=\textheight]{\mssPathDef/svn_work/Consulting_Business/VG_Visuals/IncludeLogos/MOORE_LOGO_Tagline.pdf} \includegraphics[width=\textwidth]{\mssPathDef/svn_work/Consulting_Business/VG_Visuals/IncludeLogos/MOORE_LOGO_Tagline.png} \includegraphics[scale=3.0]{\mssPathDef/svn_work/Consulting_Business/sales_presentations/business_cards/images/bwmoore_d600.png} \end{center} \end{titlepage} \tableofcontents \listoffigures \end{document}

Figure 3 has an example of how to prepare all of the major items in a large document, but this is just touching the tip of the iceberg on what can be done with LaTeX. The main unusual feature in this example is the /ifwindows segment; this sets up a variable with a path structure for my windows laptop or my Ubuntu desktop, so that I can work on the files on either machine.

For more information on LaTeX tags, read one or more of the following tutorials and books:

Call all Needed Libraries in the Prologue knitr Chunk

Next, add the "prologue" in the file where all of the required R libraries are included and options are set:

<<prologue,message=FALSE,echo=FALSE,error=FALSE,warning=FALSE>>= #install.packages(c("dplyr","magrittr","assertr","ggvis","googleVis","htmlwidgets","networkD3","rCharts","qtlcharts","dygraphs","d3heatmap")) require(ggplot2) require(dplyr) require(magrittr) require(assertr) require(ggplot2) require(fImport) require(reshape2) require(scales) #library(RCurl) setwd("~/svn_work/Consulting_Business/web_site/articles/econometric_charts/src/R") options('download.file.method'='wget') read_chunk("econometric_charts_chunks.R") @

It does not matter where you put this as long as it occurs before the first slide that uses R to generate the output. 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 including code into Markdown presentations and reports.

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, .Rmd, .RPres 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.

Writing the Knitr Chunk File econometric_charts_chunks.R With an Interactive Graphic

The previous articles in this series talk about how to create the chunk file for reuse; see R Reporting Part 4: Using Markdown for Presentations for instructions on creating the chunk file.

Create a Section in the Article and Call an R Chunk to Create a Graphic

Once the chunk file is ready, you can call it from your article:

\section{Pricing} There are several econometric series available on the St. Louis Federal Reserve System FRED2 web site that are useful in setting prices for loans and deposits. This section contains plots of recent downloads of common series from FRED2. \subsection{Swap Rates} Swap rates are a useful proxy for top-of-market CD rates and for transfer prices, since an institution can both borrow and deposit funds at approximately the swap rate. Rational institutions with access to markets rarely go significantly higher than the swap rate. Typically, wholesale funders like the Federal Home Loan Bank (FHLB) offer rates that are generally about 50 basis points higher than the swap rate; it is very unusual for an institution to gather deposits at rates above these rates. <<costOfFunds,message=FALSE,echo=FALSE,error=FALSE,warning=FALSE,fig.path="images/figures",fig.cap="Swap rates are a useful proxy for top-of-market CD rates and for transfer prices.",dev=c('pdf','png','jpeg')>>= @ \footnote{Board of Governors of the Federal Reserve System (US), 1-Year Swap Rate [MSWP1], 2-Year Swap Rate [MSWP2], 3-Year Swap Rate [MSWP3], and 5-Year Swap Rate [MSWP5] retrieved from FRED, Federal Reserve Bank of St. Louis \url{https://research.stlouisfed.org/fred2/series/series_name}, \today.} % % % \newpage \section{Severity of Loss} This section contains plot of econometrics that are useful in forecasting the severity of loss when a loss occurs on loan products. \subsection{CPI for Used Cars and Trucks} The CPI for Used Cars and Trucks is useful in forecasting the severity of loss for auto loans. As the index increases, the severity of loss will decrease, while a decreasing index will potentially cause an increase in the severity of loss. Understanding the seasonality in the index can help in decision making on whether to hold a vehicle in inventory for one or two months to get a better price at auction or to sell the vehicle as quickly as possible during periods when the market is expected to decline. <<autoCPI,message=FALSE,echo=FALSE,error=FALSE,warning=FALSE,fig.path="images/figures",fig.cap="The CPI for Used Cars and Trucks is useful in forecasting the severity of loss for auto loans.",dev=c('pdf','png','jpeg')>>= @ \footnote{US. Bureau of Labor Statistics, Consumer Price Index for All Urban Consumers: Used cars and trucks [CUUR0000SETA02], retrieved from FRED, Federal Reserve Bank of St. Louis \url{https://research.stlouisfed.org/fred2/series/CUUR0000SETA02}, \today.}

Adding Mid-sentence R Output

If you are preparing a report that will have to be re-run with new data on a regular basis, you may want to use number generated by R in the report. Knitr (or Sweave) make this easy using the \Sexpr{} tag. In most cases, you will want to use the round or format functions to limit the number of digits. The I() function is mainly useful for Rhtml files, where it removes a code-like font; this is included here only because you may run across it in files that have been converted from .Rhtml to .Rnw.

\section{Summary} <<sumP,message=FALSE,echo=FALSE,error=FALSE,warning=FALSE,fig.path="images/figures",fig.cap="Mortgage Delinquency is useful in forecasting frequency of loss.",dev=c('pdf','png','jpeg')>>= 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 swpDf$dateVal <- as.Date(swpDf$dateVal) # ggplot's scale_x_date doesn't work with character data types colnames(swpDf)[2] <- "Series" max5year <- mean(swpDf$MSWP5) max1year <- mean(swpDf$MSWP1) summary(swpDf) @ There are several different ways to format output for in-line R: \begin{itemize} \item The mean rate in the 5-year swap series is \Sexpr{mean(swpDf$MSWP5)}. \item The mean rate in the 5-year swap series is \Sexpr{round(mean(swpDf$MSWP5),2)}. \item The mean rate in the 5-year swap series is \Sexpr{format(round(mean(swpDf$MSWP5),2), nsmall=0)}. \item The mean rate in the 5-year swap series is \Sexpr{I(format(round(mean(swpDf$MSWP5),2), nsmall=0))}. \end{itemize}

Adding the Epilogue

Finally, if there are things that you want to do after the R code for your graphics run and before pdflatex runs, add a epilogue to run additional commands. This is a personal coding practice and is not a standard or requirement. I usually run optipng or some other image compression utility as the last step in all of my Rhtml, Rmd and Rnw files:

<<epilogue,message=FALSE,echo=FALSE,error=FALSE,warning=FALSE,fig.path="images/figures",out.width='\\textwidth',out.height='0.8\\textheight'>>= system('optipng images/figures/*.png') @

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 4. Running knitr to generate the PDF file and all figures.
Running knitr in RStudio to generate a PDF file from the Rnw source file

The output from this will be a PDF file that you can view on any machine without any dependencies. You cannot do interactive visualizations in Beamer.