Mastering R Presentations - R-Bloggers
Mastering R Presentations - R-Bloggers
R-BLOGGERS
R news and tutorials contributed by hundreds of R bloggers
Search R-bloggers.. Go
Mastering R presentations
Your e-mail here Subscribe
Posted on September 23, 2019 by R on Coding Club UC3M in R bloggers | 0 Comments
Share Tweet
by Paula LC
Do you want to know how to make elegant and simple reproducible presentations? In
this talk, we are going to explain how to do presentations in di erent output formats
Most viewed posts (weekly)
using one of the easiest and most exhaustive statistical software, R. Now, it is possible
create Beamer, PowerPoint, or HTML presentations, including R code, LT
A
EX equations, PCA vs Autoencoders for Dimensionality
graphics, or interactive content. Reduction
7 Must-Have Skills to Get a Job as a Data
After the tutorial, you will be able to create R presentations on your own with R
Scientist
Markdown in RStudio. But don’t worry if you don’t know a lot of R Markdown, because
Shiny without Shiny: RTutor in RStudio's new
it’s really simple to use it with RStudio and you will discover the keys to master the
Visual Markdown Mode
language.
How to write the rst for loop in R
Should you learn Python or R in 2021?
We have several options to create amazing technical presentations in pdf format with
5 Ways to Subset a Data Frame in R
tools like PowerPoint or LT
A
EX . But the truth is that when we want to generate a full
Correlation Analysis in R, Part 2: Performing
and complete document with graphs, code, and text, then we invest more time in the
and Reporting Correlation Analysis
appearance than in the content itself, or learning how to add content easily. So here I
want to show you a good alternative using R. The best feature R has is the exibility and
simplicity of the code to reproduce amazing presentations with little work. To achieve it, Sponsors
R uses Markdown. But, what is exactly Markdown?
What is Markdown?
Markdown is a simple language to write web-based content easy both for writing and
reading. The key is that it can be converted to many output formats with a simpli ed
syntax.
Basics of Markdown
Headers
# H1
## H2
### H3
#### H4
##### H5
###### H6
Alt-H1
======
Alt-H2
------
Emphasis
https://www.r-bloggers.com/2019/09/mastering-r-presentations/ 1/11
16/04/2021 Mastering R presentations | R-bloggers
*italics* or _italics_.
**bold** or __bold__.
~~Strikethrough~~
Lists
Links
Images
Inline-style:

Blockquotes
TeX Formula
Besides these basics, you can to add tables, rulers, links to videos, HTML code, etc. To
know more visit the creator’s web site: https://daring reball.net/projects/markdown/ or
this cheatsheet https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet.
R has a speci c le format for this type of documents .Rmd. R Markdown has an online
book really useful and detailed here https://bookdown.org/yihui/rmarkdown/. This tool
let you build di erent type of documents like the next ones:
Documents: HTML, LT
A
EX /PDF, Microsoft Word, Tufte-style handouts
How to start
The rst step is to get R and RStudio, and install the package rmarkdown with the code
install.packages("rmarkdown")
https://www.r-bloggers.com/2019/09/mastering-r-presentations/ 2/11
16/04/2021 Mastering R presentations | R-bloggers
So going to File -> New File -> R Markdown and selecting the option Presentation
, you are going to have di erent options to create your slides. Selecting any of them, a
le like this is automatically generated:
Depending on the nal style of the output there are di erent output options. In the next
points, we are going to explain in detail the main features of all them.
The header is the R Markdown document part where you can set the title, the author,
the date, and the output as the image shows: Recent Posts
---
What is a sun ower plot?
title: "Mastering R presentations"
author: "Paula LC" new order
date: "2019-09-24" Cross-origin requests with beakr
output: ioslides_document
The 2021 RUGS Program is Live!
---
Visualising stock prices and volume by
But at the same time, other options can be determined as follows: @ellis2013nz
Introduction to lea egend
--- Cracking and packing: a simple gerrymander
title: "Mastering R presentations"
simulation
author: "Paula LC"
date: "2019-09-24" What’s New on RStudio Cloud – February
output: 2021
ioslides_presentation:
Some thoughts on rstudio::global talks
incremental: false
self-included: true Simple audio classi cation with torch
--- hard birthday problem
Network trees: networktree 1.0.0, web page,
Content and Psychometrika paper
Hands-on R and dplyr – Analyzing the
Once the header is completed, you can add any kind of content that you can practically Gapminder Dataset
imagine: R code, equations, charts, images, videos etc. In the next points we are going to Use Your R Skills to Fight COVID-19 – COVID-
see how to add each type of content. 19 Challenge Funded by R Consortium – Open
Now!
R Chunks Little useless-useful R functions – R Version
In addition to plain text, headers and other Markdown elements, you have the option of
inserting R code which will be executed every time you run the le. These parts of the Jobs for R-users
document are called R chunks. To insert an R Chunk you can use RStudio toolbar
Insert button or the keyboard shortcut Ctrl + Alt + I on Windows and Junior Data Scientist / Quantitative economist
default, the code is not shown. Data Analytics Auditor, Future of Audit Lead
@ London or Newcastle
More options:
python-bloggers.com
cache: cache results for future knits (default = FALSE)
(python/data-science news)
cache.path: directory to save cached results in (default = “cache/”)
child: le(s) to knit and then include (default = NULL)
How to Deploy ML Models into AWS with
collapse: collapse all output into single block (default = FALSE)
Elastic Beanstalk
comment: pre x for each line of results (default = ‘##’)
How a File Format Exposed a Crossword
dependson: chunk dependencies for caching (default = NULL)
Scandal
echo: Display code in output document (default = TRUE)
PyTorch + SHAP = Explainable Convolutional
engine: code language used in chunk (default = ‘R’)
Neural Networks
https://www.r-bloggers.com/2019/09/mastering-r-presentations/ 3/11
16/04/2021 Mastering R presentations | R-bloggers
error: Display error messages in doc (TRUE) or stop render when errors occur Hack: elif in list comprehension
(FALSE) (default = FALSE) Add Text On An Image using Python
eval: Run code in chunk (default = TRUE) Parsing portfolio optimization
message: display code messages in document (default = TRUE) Stocks Market Beta with Rolling Regression
results=asis: passthrough results
results=hide: do not display results Full list of contributing R-bloggers
results=hold: put all results below all code
tidy: tidy code for display (default = FALSE)
Archives
warning: display code warnings in document (default = TRUE)
g.align: ‘lef’, ‘right’, or ‘center’ (default = ‘default’) Select Month
g.cap: gure caption as character string (default = NULL)
g.height, g.width: Dimensions of plots in inches
highlight: highlight source code (default = TRUE) Other sites
include: Include chunk in doc after running (default = TRUE)
Jobs for R-users
In the next link you can nd more details about R chunks: SAS blogs
https://bookdown.org/yihui/rmarkdown/r-code.HTML
Equations
There is the chance to add equations to your presentations with MathJax scripts. These
are included in HTML documents for rendering LT
A
EX and MathML equations. To
default to use an HTTPS URL from a CDN host (currently provided by RStudio)
local : to use a local version of MathJax (which is copied into the output
directory). Note that when using “local” you also need to set the self_contained
option to false.
URL indicating the location to load MathJax
null to exclude MathJax entirely.
---
title: "Example"
output:
ioslides_document:
mathjax: local
self_contained: false
---
---
title: "Example"
output:
ioslides_document:
mathjax: "http://example.com/MathJax.js"
---
Tables
You have four options to add tables. First one, directly from R Markdown
or the next ones, from R code with the libraries knitr , xtable , or stargazer .
https://www.r-bloggers.com/2019/09/mastering-r-presentations/ 4/11
16/04/2021 Mastering R presentations | R-bloggers
Interactive graphs
In R there are a lot of packages to create interactive graphs. Highcharter is one of them,
as well as the well-known HTMLwidgets. Here we have an example of a highcharter
graph.
library(highcharter)
highchart() %>%
hc_title(text = "Scatter chart with size and color") %>%
hc_add_series(mtcars, "point", hcaes(wt, mpg, size=drat, color=hp)) %>%
widgetframe::frameWidget()
R new
Presentation formats
We just explored the di erent contents and parts of our R Markdown document. Let’s
see what type of output format we can obtain.
Ioslides
---
output:
ioslides_presentation:
incremental: true
---
Moreover, you can change the presentation size, the text size, or even the transition
speed in the header of the document. Speci cally, for the transition speed you can set
the number of seconds for each slide or use the standard options: default , slower ,
faster .
---
output:
ioslides_presentation:
widescreen: true
smaller: true
transition: slower
https://www.r-bloggers.com/2019/09/mastering-r-presentations/ 5/11
16/04/2021 Mastering R presentations | R-bloggers
---
For another hand, there is a quick way to add a background image without editing the
CSS le,
## {data-background="my_background.png"}
But if you want to add speci c style changes to your presentation, I recommend you to
edit the CSS le and add it to the header of the RMarkdown document:
---
output:
ioslides_presentation:
css: styles.css
---
Slidy
Slidy has more exibility than ioslides as to appearance and style. Now we are going to
see some of the main special features that slidy has.
There is the chance to change the display mode with the next shortcuts;
And we can adjust the font directly in the header of the document without editing the
CSS le:
---
output:
slidy_presentation:
font_adjustment: -1
---
You will nd other interesting features of slidy such as the countdown timer in the footer
or the customized footer text that can be easily added with the options duration and
footer .
Slidy themes
In slidy, there are di erent Boostrap themes to use drawn from the Bootswatch theme
library. The themes are default , cerulean , journal , flatly , darkly ,
readable , spacelab , united , cosmo , lumen , paper , sandstone , simplex ,
and yeti . To add your own style with a CSS le, pass null in the theme parameter.
Moreover, the syntax highlighting style can be speci ed with the option highlight .
Supported styles are default , tango , pygments , kate , monochrome , espresso ,
zenburn , haddock , and textmate . And you have the option of preventing syntax
highlighting passing null to the parameter.
Beamer
Beamer is a LT
A
EX class to produce presentations and slides. It is so common in
academia and so useful to add mathematical formulas and expressions. You can create
your own Beamer presentations from R without a deep knowledge of LT
A
EX (only
Markdown).
So the rst step is to install tex. Tex is a typesetting for complex mathematical formulae
used in LT
A
EX . To install it, download tone of the next programs, depending on your OS
https://www.r-bloggers.com/2019/09/mastering-r-presentations/ 6/11
16/04/2021 Mastering R presentations | R-bloggers
Beamer themes are the same that you can nd in LTEX . In the next link
A
---
output:
beamer_presentation:
theme: "AnnArbor"
colortheme: "dolphin"
fonttheme: "structurebold"
---
Reveal.js
There are other interesting options to create presentations in R such as reveal.js and
xaringan. Reveal is very well-known because of the exibility in the themes and
transitions by default, the vertical slides or the possibility to include a web site inside a
slide. In this part, we are going to explain how to generate a revealjs le and the main
features of this awesome library.
install.packages("revealjs")
Then, you can directly change in the R Markdown document header the output argument
to revealjs_presentation or go to menu
File -> New File -> R Markdown -> From template and select reveal.js presentation.
- f for fullscreen - o or ESC for overview mode - alt or ( ctrl in Linux) and click
an element, to zoom this element - s for speaker view (so pretty!) - B or . to
pause the presentation
And there is a lot of variety about appearance and styles. If you want to change how the
presentation looks like, you can choose any of the next theme options: default ,
simple , sky , beige , serif , solarized , blood , moon , night , black ,
league , and white . And for the syntax highlighting style: default , tango ,
pygments , kate , monochrome , espresso , zenburn , and haddock . Pass null to
prevent syntax highlighting. The way to specify it is the same than the previous
presentation types.
---
output:
revealjs::revealjs_presentation:
theme: black
highlight: pygments
https://www.r-bloggers.com/2019/09/mastering-r-presentations/ 7/11
16/04/2021 Mastering R presentations | R-bloggers
---
In revealjs you can center the text of the slides changing the center option to true,
which by default is false, as well as the possibility of modifying the transitions and
backgrounds, i.e. how the slide is going to move to the next one. Available transitions
and background_transitions are default , fade , slide , convex , concave ,
zoom or none . Any of these global options can be overriden specifying the data-
transition attribute in the header of the slide:
Moreover, Revealjs lets add di erent backgrounds like color, image, video, and iframe:
Finally, you can specify the level of heading will be used with the slide_level option.
For example, if the slide_level is 2, the level-1 headers will be built horizontally and level-
2 headers, vertically.
Other interesting features are the great look on touch devices, the fragmented slides,
easy to export to pdf, keyboard bindings, or the parallax scrolling background.
References https://CRAN.R-project.org/package=revealjs.
Ninja presentation
The last type of presentations that we are going to see is the xaringan library. It is an R
Markdown extension based on the JavaScript library remark.js (https://remarkjs.com).
This package was originally designed for “ninja”, so it is recommended to people that
have a well-known of CSS. For another hand, if you need slides to be self-contained, then
xaringan it is not a good option because needs a webserver to run. Another bad news is
that xaringan doesn’t work well with HTML widgets.
install.packages("xaringan")
or install it directly from GitHub to ensure that you are downloading the last version
devtools::install_github('yihui/xaringan')
https://www.r-bloggers.com/2019/09/mastering-r-presentations/ 8/11
16/04/2021 Mastering R presentations | R-bloggers
Note: If you understand chinese you can select the last option ;).
---
title: "Presentation Ninja"
subtitle: "<img draggable="false" role="img" class="emoji" alt=" " src="https
author: "Yihui Xie"
institute: "RStudio, Inc."
date: "2016/12/12 (updated: 2019-09-23)"
output:
xaringan::moon_reader:
lib_dir: libs
nature:
highlightStyle: github
highlightLines: true
countIncrementalSlides: false
---
A lit bit more complicated than others and as you will see, there are some funny
arguments that make this library really di erent.
Besides the options provided by remark.js, there are others such interesting like
autoplay the slides or the countdown timer. Slides can be automatically played setting
the autoplay option under nature (in milliseconds). For example, to display slides
every 30 seconds and see the countdown timer:
---
output:
xaringan::moon_reader:
nature:
autoplay: 30000
countdown: 30000
---
https://www.r-bloggers.com/2019/09/mastering-r-presentations/ 9/11
16/04/2021 Mastering R presentations | R-bloggers
It is possible to highlight code lines turning the option highlightLines to true or to
extend the markdown syntax de ning custom macros with the beforeInit option
under the option nature.
body {
background: #111;
background-color: #111; }
.reveal {
color: #fff; }
.reveal h1,
.reveal h2,
.reveal h3,
.reveal h4,
.reveal h5,
.reveal h6 {
color: #fff;
}
.reveal h1 {
font-size: 4.77em; }
Then you have to save the CSS le in the same path that your R presentation document.
Basic example
Finally, let’s show you a simple reveal.js example to get you started.
---
title: "Revealjs Presentation Example"
author: "Paula LC"
date: "September 24, 2019"
output:
revealjs::revealjs_presentation:
theme: black
highlight: pygments
---
Hello world!
- List item 1
- List item 2
+ List item 2.1
## Equations
## Tables
https://www.r-bloggers.com/2019/09/mastering-r-presentations/ 10/11
16/04/2021 Mastering R presentations | R-bloggers
mod <- lm(dist~speed, data=cars)
pander::pander(mod, type = 'html', title = 'Table with pander')
```
## Interactive plot
IVE PLOT
Conclusions
RStudio is an awesome framework that provides you the chance to create nice
presentations with a simple syntax, adding interactive content, and with a professional
and modern style. Moreover, your presentation will be reproducible if you want to make
any change, as well as you can save your templates to use them in the future. In my
opinion, it is a really good alternative to other traditional software to create
presentations and so easy to work with it. I hope it is so useful for you too
Related
Share Tweet
To leave a comment for the author, please follow the link and comment on their blog: R on
Coding Club UC3M.
R-bloggers.com o ers daily e-mail updates about R news and tutorials about learning R and
many other topics. Click here if you're looking to post or nd an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
https://www.r-bloggers.com/2019/09/mastering-r-presentations/ 11/11