R Reporting Part 5: Using LaTeX/Beamer for PDF Presentations

This is the fifth 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 Beamer presentations allow easy use of math symbols, cross referencing, citation, and a create presentation table of contents. To create a LaTeX/Beamer presentation 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 Beamer presentation, 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 Beamer presentation file with title page
\documentclass{beamer} \title[Selected Econometrics]{Selected Econometrics for \today} \subject{Selected Econometrics for \today} %\copyright{Copyright \copyright 2014 by Moore Software Services, LLC. All rights reserved.} \author[Bruce Moore]{Bruce Moore, DEng} \institute[Moore Software Services]{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.} \\ \texttt{www.mooresoftwareservices.com} \\ } \date{\today} \begin{document} % % Titlepage % \begin{frame}[plain] \titlepage \end{frame} %\SweaveOpts{concordance=TRUE} \section{Swap} % % % \begin{frame}[fragile]{Heading for slide 1} \begin{itemize} \item Item 1 \item Item 2 \end{itemize} \end{frame} \end{document}

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 Slide and Call an R Chunk to Create a Graphic

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

% % % \begin{frame}[fragile]{Swap rates are a useful proxy for top-of-market CD rates and for transfer prices} <<costOfFunds,message=FALSE,echo=FALSE,error=FALSE,warning=FALSE,fig.path="images/figures",out.height='0.6\\textheight'>>= @ \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.} \end{frame}

Adding the Epilog

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.