R Reporting Part 4: Using Markdown for Presentations
This is the fourth 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:
- R Reporting Part 1: Tools
- R Reporting Part 2: Choosing the Right Markup for the Task
- R Reporting Part 3: Using Rhtml for Batch Web Reporting
- R Reporting Part 4: Using Markdown for Interactive Presentations
- R Reporting Part 5: Using LaTeX/Beamer for PDF Presentations
- R Reporting Part 6: Using LaTeX for PDF Articles
- R Reporting Part 7: Converting R Documents to E-books
- R Reporting Part 8: Using LaTeX to Create Posters
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:
- Econometric graphs in PDF form for use as slides on a projector
- Econometric graphs in PDF form for printing
- Econometric graphs in EPUB format
- Econometric graphs in Kindle AZW3 format
- Econometric graphs in A0 poster format
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 Rmd for Interactive Presentations
The first step in creating the interactive presentation is to create the .Rmd
file in RStudio as shown in Figure 1. RStudio will prompt you for the output type as shown in Figure 2, where there are three choices:
- HTML (ioslides)–this type allows interactive graphics and math markup (using MathJax). The math markup is not portable to other machines unless the machine has either an Internet connection or has MathJax installed.
- HTML (Slidy)–this type allows interactive graphics and math markup (using MathJax). The math markup is not portable to other machines unless the machine has either an Internet connection or has MathJax installed.
- PDF (Beamer)–this type does not allow interactive graphics and math markup (using LaTeX). The math markup in the PDF file is portable to other machines.
You can change the output type later if you want.


Modify the Title and Other Header Information
The first step is to modify the header information in the Markdown file to set the title, author, output type and footer. The snippet below shows the major header settings that you are likely to need for a basic presentation. The incremental
setting allows you to step through the bullet points in a presentation. The use of “:” and indention in the header is syntactically important; the “:” is a line end terminator.
---
title: "Using RStudio for Presentations and Reports"
author: "Bruce Moore"
date: "February 26, 2016"
output:
ioslides_presentation:
incremental: true
footer: "Bruce Moore, Moore Software Services"
---
The Markdown Reference Guide has complete information on the options that you can include in the header.
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:
‘‘‘{r,prologue,message=FALSE,echo=FALSE,error=FALSE,warning=FALSE}
require(ggplot2)
require(dplyr)
require(magrittr)
require(assertr)
require(ggplot2)
require(fImport)
require(reshape2)
require(scales)
require(googleVis)
#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")
‘‘‘
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.
Create a Slide and call the R Chunk to Generate the Graphic
The next step is to add the ##
tag followed by the slide heading and other Markdown tags for a slide. This is followed by the knitr
markup to call our code chunk from the econometric_charts_chunks.R
file:
‘‘‘{r, costOfFunds,fig.path="images/figures/",fig.width=7, fig.height=5,fig.dpi=100,message=FALSE,warning=FALSE,echo=FALSE,cache=FALSE}
‘‘‘
All R code in the .Rmd
file must be enclosed with the knitr
tags:
‘‘‘{r, }
and
‘‘‘
This is a special Markdown code delimiters that knitr
looks uses to identify 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.
Writing the Knitr Chunk File econometric_charts_chunks.R With an Interactive Graphic
The last step is to add the R code to the generate the interactive graphic to the econometric_charts_chunks.R
so that knitr
will generate the graphic. In the .R
file, the costOfFundsInteractive
## ---- costOfFundsInteractive ----
label for the chuck must match the chunk label ## ---- costOfFundsInteractive ----
in the econometric_charts_chunks.R
file:
## ---- costOfFundsInteractive ----
#
# Make the figure with swap rates
#
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.Date(swpDf$dateVal) # ggplot's scale_x_date doesn't work with character data types
colnames(swpDf)[2] <- "Series"
chart <- googleVis::gvisLineChart(data=swpDf
,xvar="dateVal",yvar=c("MSWP5","MSWP2","MSWP1")
,options=list(width=800,height=600,gvis.editor="Edit",title='Swap Rates'))
#plot(chart) # use this for console debugging
print(chart) # use this for output in Markdown
In the .Rmd
file
While debugging in the console, uncomment the plot
command; for output in knitr
use the plot
statement.
Adding the Epilog
The final chunk in the presentation file is probably a good practice or perhaps a time saving measure but is not a requirement; an epilogue to run image compression on any image files that were created by the script:
‘‘‘{r, epilogue,echo=FALSE,message=FALSE,warning=FALSE,cache=FALSE}
system('optipng("images/figures/*.png")')
‘‘‘
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:
knitr
to generate the HTML file and all figures.
The output from this will be an HTML file that you can open in a browser. If you use interactive visualizations that depend upon libraries from the web, you may have problems running your presentation without an Internet connection, so make sure to test your presentation with networking turned off to understand what will and will not render or work.