Dear all,
This week we’ll extend the knowledge we have about rmarkdown
to generate presenations in html
and pdf
format. We’ve seen in week 4 that RStudio
’s markdown
implementation is a very quick and convenient language to quickly mark up text documents and, if necessary, to include images, mathematical equations (\(\LaTeX\)) and to display and/or execute code (R
, Python
, etc).
Let’s take rmarkdown
a step further.
Best,
Gerko
rmarkdown
for presentationsGenerating presentations with markdown
is very much like marking up regular documents in markdown
. However, there are some minor differences:
First, presentations usually follow some form of dimensionality that adheres to display conventions: screen dimensions are historically obeying to 4:3
or 16:9
aspect ratios, although recent trends do allow for wider 18:9
or 21:9
aspect ratios. The benefit of marking up regular html
documents with rmarkdown
is the natural scaling of content to the device. With pdf
’s the resulting document is paginated and follows a strict canvas size (e.g. A4, US letter, etc) suitable for printing.
Second, output options need to be explicitly formulated. There are multiple implementations for generating html
presentations and, when the output is set to pdf
, the beamer
specifications have to be declared.
Third, the pound sign #
is still used for (sub)sections, but the leveling is very important: a single #
indicates a new chapter in your presentation, a double ##
indicates a new slide within that chapter.
RStudio supports the following slide output styles:
html
ioslides: My preferred mode of creating presentations. See this page for a detailed how-to.html
slidy: Another flexible mode of creating presentations. See this page for a detailed how-to.pdf
beamer. See this page for a detailed how-to.I suggest you take a look at these output styles and pick a style. You can always switch later on and have your rmarkdown
document generate a different style.
See this page on a detailed walkthrough about how rmarkdown
handles bibliographies and citations.
With package plotly
, any graphics generated by ggplot2
can become interactive in html
output files. It is adviced to install the developer version of ggplot2
from GitHub
. You can do so by running
devtools::install_github('hadley/ggplot2')
An example:
require(plotly)
set.seed(123)
df <- data.frame(x <- rchisq(1000, 5, 10),
group <- sample(LETTERS[1:5], size = 1000, replace = T))
p <- ggplot(df, aes(x, fill = group)) +
geom_density(alpha = 0.5, position = "stack") +
ggtitle("stacked density chart")
ggplotly(p)
With package DT
, any table in R
can become interactive in html
output files. An example that outputs the first 5 rows by default:
require(DT)
datatable(mtcars, options = list(pageLength = 5))
DiagrammeR
it is straightforward to generate flow charts from within any rmarkdown
document. An example flowchart:
library(DiagrammeR)
grViz("
digraph {
layout = twopi
node [shape = circle]
A -> {B C D}
B -> {C D}
D -> {A B}
}")
Or, more complicated:
grViz("
digraph dot {
graph [compound = true, nodesep = .5, ranksep = .25,
color = crimson, label='Polynomial Regression Model'
/*, rankdir='LR', style=filled, fillcolor = blue*/
]
subgraph cluster1 {
node [shape = diamond,
color = black]
xi
node [shape = circle,
style = filled,
fillcolor = grey]
ti
edge [color = black]
xi -> ti
label='N'
}
node [shape = circle]
w
node [shape = diamond,
color = black,
label = 'α']
alpha
node [shape = diamond,
color = black,
label = 'σ²']
sigma
edge [color = black]
alpha -> w
w -> ti
sigma -> ti
}
",
engine = "dot")
An example diagram sequence with DiagrammeR
:
DiagrammeR("
sequenceDiagram;
customer->>ticket seller: ask ticket;
ticket seller->>database: seats;
alt tickets available
database->>ticket seller: ok;
ticket seller->>customer: confirm;
customer->>ticket seller: ok;
ticket seller->>database: book a seat;
ticket seller->>printer: print ticket;
else sold out
database->>ticket seller: none left;
ticket seller->>customer: sorry;
end
")
htmlwidgets
for R
Have a look at the htmlwidgets
website to lear all about referencing, tweaking, linking, using and misusing a variety of html widgets in rmarkdown
.
Create a presentation about any topic you like that consists of at least 7 slides and includes all of the following:
Of course this presentation may serve as the basis for presentations that you may have to create for other courses. Submit the presentation to this year’s GitHub repo.