revealjg
Overview
This repository provides an R Markdown custom format for reveal.js HTML presentations.
You can use this format in R Markdown documents by installing this package as follows:
remotes::installgithub("jonathan-g/revealjg")To create a reveal.js presentation from R Markdown you specify the revealjs_presentation output format in the front-matter of your document. You can create a slide show broken up into sections by using the # and ## heading tags (you can also create a new slide without a header using a horizontal rule (----). For example here’s a simple slide show:
---
title: "Habits"
author: John Doe
date: March 22, 2005
output: revealjg::revealjs_presentation
---
# In the morning
## Getting up
- Turn off alarm
- Get out of bed
## Breakfast
- Eat eggs
- Drink coffee
# In the evening
## Dinner
- Eat spaghetti
- Drink wine
## Going to sleep
- Get in bed
- Count sheepRendering
Depending on your use case, there are 3 ways you can render the presentation.
- RStudio
- R console
- Terminal (e.g., bash)
RStudio
When creating the presentation in RStudio, there will be a Knit button right below the source tabs. By default, it will render the current document and place the rendered HTML file in the same directory as the source file, with the same name.
Note: Unlike the the other slideshow outputs, the slideshow viewer popup from RStudio will be blank, to view the slide show click the open in browser button, and the slide show will render in your default web browser.
R Console
The Knit button is actually calling the rmarkdown::render() function. So, to render the document within the R console:
rmarkdown::render('my_reveal_presentation.Rmd')There are many other output tweaks you can use by directly calling render. You can read up on the documentation for more details.
Display Modes
The following single character keyboard shortcuts enable alternate display modes:
'f'enable fullscreen mode'o'enable overview mode
Pressing Esc exits all of these modes.
Incremental Bullets
You can render bullets incrementally by adding the incremental option:
If you want to render bullets incrementally for some slides but not others you can use this syntax:
Appearance and Style
There are several options that control the appearance of revealjs presentations:
themespecifies the theme to use for the presentation (available themes are “default”, “simple”, “sky”, “beige”, “serif”, “solarized”, “blood”, “moon”, “night”, “black”, “league” or “white”).highlightspecifies the syntax highlighting style. Supported styles include “default”, “tango”, “pygments”, “kate”, “monochrome”, “espresso”, “zenburn”, and “haddock”. Pass null to prevent syntax highlighting.centerspecifies whether you want to vertically center content on slides (this defaults to false).smartindicates whether to produce typographically correct output, converting straight quotes to curly quotes,---to em-dashes,--to en-dashes, and...to ellipses. Note thatsmartis enabled by default.
For example:
Slide Transitions
You can use the transition and background_transition options to specify the global default slide transition style:
transitionspecifies the visual effect when moving between slides. Available transitions are “default”, “fade”, “slide”, “convex”, “concave”, “zoom” or “none”.background_transitionspecifies the background transition effect when moving between full page slides. Available transitions are “default”, “fade”, “slide”, “convex”, “concave”, “zoom” or “none”.
For example:
You can override the global transition for a specific slide by using the data-transition attribute, for example:
## Use a zoom transition {data-transition="zoom"}
## Use a faster speed {data-transition-speed="fast"}You can also use different in and out transitions for the same slide, for example:
Slide Backgrounds
Slides are contained within a limited portion of the screen by default to allow them to fit any display and scale uniformly. You can apply full page backgrounds outside of the slide area by adding a data-background attribute to your slide header element. Four different types of backgrounds are supported: color, image, video and iframe. Below are a few examples.
## CSS color background {data-background=#ff0000}
## Full size image background {data-background="background.jpeg"}
## Video background {data-background-video="background.mp4"}
## Embed a web page as a background {data-background-iframe="https://example.com"}Backgrounds transition using a fade animation by default. This can be changed to a linear sliding transition by specifying the background-transition: slide. Alternatively you can set data-background-transition on any slide with a background to override that specific transition.
2-D Presentations
You can use the slide_level option to specify which level of heading will be used to denote individual slides. If slide_level is 2 (the default), a two-dimensional layout will be produced, with level 1 headers building horizontally and level 2 headers building vertically. For example:
With this layout horizontal navigation will proceed directly from “Horizontal Slide 1” to “Horizontal Slide 2”, with vertical navigation to “Vertical Slide 1”, etc. presented as an option on “Horizontal Slide 1”.
Reveal Options
Reveal.js has many additional options to configure it’s behavior. You can specify any of these options using reveal_options, for example:
---
title: "Habits"
output:
revealjg::revealjs_presentation:
self_contained: false
reveal_options:
slideNumber: true
previewLinks: true
---You can find documentation on the various available Reveal.js options here: https://github.com/hakimel/reveal.js#configuration.
Figure Options
There are a number of options that affect the output of figures within reveal.js presentations:
fig_widthandfig_heightcan be used to control the default figure width and height (7x5 is used by default)fig_retinaSpecifies the scaling to perform for retina displays (defaults to 2, which currently works for all widely used retina displays). Note that this only takes effect if you are using knitr >= 1.5.21. Set tonullto prevent retina scaling.fig_captioncontrols whether figures are rendered with captions
For example:
MathJax Equations
By default MathJax scripts are included in reveal.js presentations for rendering LaTeX and MathML equations. You can use the mathjax option to control how MathJax is included:
Specify “default” to use an https URL from the official MathJax CDN.
Specify “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_containedoption to false.Specify an alternate URL to load MathJax from another location.
Specify null to exclude MathJax entirely.
For example, to use a local copy of MathJax:
---
title: "Habits"
output:
revealjg::revealjs_presentation:
mathjax: local
self_contained: false
---To use a self-hosted copy of MathJax:
---
title: "Habits"
output:
revealjg::revealjs_presentation:
mathjax: "http://example.com/mathjax/MathJax.js?config=TeX-AMS-MML_HTMLorMML"
---To exclude MathJax entirely:
Document Dependencies
By default R Markdown produces standalone HTML files with no external dependencies, using data: URIs to incorporate the contents of linked scripts, stylesheets, images, and videos. This means you can share or publish the file just like you share Office documents or PDFs. If you’d rather keep dependencies in external files you can specify self_contained: false. For example:
Note that even for self contained documents MathJax is still loaded externally (this is necessary because of it’s size). If you want to serve MathJax locally then you should specify mathjax: local and self_contained: false.
One common reason keep dependencies external is for serving R Markdown documents from a website (external dependencies can be cached separately by browsers leading to faster page load times). In the case of serving multiple R Markdown documents you may also want to consolidate dependent library files (e.g. Bootstrap, MathJax, etc.) into a single directory shared by multiple documents. You can use the lib_dir option to do this, for example:
Reveal Version
Reveal.js underwent a serious refactoring between major versions 4 and 5, so revealjg includes an option of specifying the reveal.js version to use. This affects which html template pandoc uses to render the slides and it affects the directory structure for finding .css and .js files in the reveal.js directory.
By default, revealjg uses reveal.js version 5.2.1.
Reveal Plugins
You can enable various reveal.js plugins using the reveal_plugins option. Plugins currently supported include:
| Plugin | Description |
|---|---|
| notes | Present per-slide notes in a separate browser window. |
| zoom | Zoom in and out of selected content with Alt+Click. |
| search | Find a text string anywhere in the slides and show the next occurrence to the user. |
| chalkboard | Include handwritten notes within a presentation. |
Note that the use of plugins requires that the self_contained option be set to false. For example, this presentation includes both the “notes” and “search” plugins:
---
title: "Habits"
output:
revealjs::revealjs_presentation:
self_contained: false
reveal_plugins: ["notes", "search"]
---You can specify additional options for the chalkboard and menu plugins using reveal_options, for example:
Advanced Customization
Includes
You can do more advanced customization of output by including additional HTML content or by replacing the core pandoc template entirely. To include content in the document header or before/after the document body you use the includes option as follows:
Pandoc Arguments
If there are pandoc features you want to use that lack equivalents in the YAML options described above you can still use them by passing custom pandoc_args. For example:
---
title: "Habits"
output:
revealjg::revealjs_presentation:
pandoc_args: [
"--title-prefix", "Foo",
"--id-prefix", "Bar"
]
---Documentation on all available pandoc arguments can be found in the pandoc user guide.
Shared Options
If you want to specify a set of default options to be shared by multiple documents within a directory you can include a file named _output.yaml within the directory. Note that no YAML delimiters or enclosing output object are used in this file. For example:
_output.yaml
All documents located in the same directory as _output.yaml will inherit it’s options. Options defined explicitly within documents will override those specified in the shared options file.