Show your insights in R Markdown as slides!

I found it interesting to share with you that R Markdown makes it possible to share a markdown file in many formats.

Suppose that you have to share your insights with people that don’t have the knowledge you have or even more important, with stakeholders! Then you need to use some kind of format that is friendly not only to you but to others as well. You may think about power point or something similar, isn’t it?. Today I will share information about creating amazing slides from a “.Rmd” file.

Slides with xaringan package

As you may note, knitr and pandoc are the starts here but don’t worry: you don’t have to know about them!

R Markdown document backbone

Header (YAML metadata)

A YAML header looks like this, and contains basic metadata about the document and rendering instructions (even parameters!):

---
title: Amazing title
author: your name here
output:
html_document:
toc: true
toc_float: true
params:
start_date: '2020-01-01'
end_date: '2020-06-01'
---

Narrative

# First-level header## Second-level header### Third-level header

But with R Markdown, you can go further. Pandoc syntax allows us to provide a unique identifier by following the header notation with {#id}, or attach one or more classes to a section with {.class-name}, e.g.,

## Second-level header {#introduction .important .large}

With IDs you can use cross-references! (see this book section with examples)

Code chunks

```{r chunk-x}
x <- 1
print(x)
```

That means that you can write R code in your R Markdown file and the output will be shown in your output file. Even the code can be shown.

Code chunks also have analogous concepts to the classes and unique identifiers that we explored for narratives. The chunk header ```{r summary-stats, class.output = ‘large-text’} gives this chunk a label summary-stats, and the class large-text for the text output blocks.

If you want to hide source code:

```{r, echo=FALSE}
1 + 1
```

To hide warning messages:

```{r, warning=FALSE}
# this will generate a warning but it will be suppressed
1:2 + 1:3
```

Body

# Title## Section X
Some introduction and then some R code chunk
```{r chunk-x}
x <- 1 print(x)
```
### Subsection X.1
Some text here.
### Subsection X.2
More text.
## Section Y
Another section.
```{r chunk-y}
y <- 2 print(y)
```
### Subsection Y.1
Some another text here.

R Markdown to create slides

“HTML may be a little weak in typesetting for paged output. However, it is much more powerful in presenting results, especially when combined with CSS and JavaScript. For example, you can embed interactive applications in HTML, and dynamically modify the appearance and even the content of an HTML page” [1]

You will be able to do some special things with HTML, such as:

  • Apply custom CSS format
  • Style code blocks and text output
  • Make code blocks scrollable
  • Put content in tabs
  • Use a custom HTML template (that’s actually amazing, isn’t it?)
  • Sharing HTML output on the web (with some R-specific services as well as GitHub/GitLab pages and Netlify)

There is also an important player when exporting to HTML: remark.js. This is a simple, in-browser, Markdown-driven slideshow tool targeted at people who know their way around HTML and CSS, featuring:

  • Markdown formatting, with smart extensions
  • Presenter mode, with cloned slideshow view
  • Syntax highlighting, supporting a range of languages
  • Slide scaling, thus similar appearance on all devices/resolutions
  • Touch support for smartphones and pads, i.e. swipe to navigate slides

Then, remark.js is the core to create your slides. The code structure is the following:

Slide 1 content
???
Slide 1 notes
---
Slide 2 content
???
Slide 2 notes

You want even more?

  • Add an overview of your presentation with a tile view
  • Make your slides editable
  • Share your slides in style with share again
  • Broadcast your slides in real-time to viewers with broadcast
  • Announce slide changes with a subtle tone
  • Animate slide transitions with animate.css
  • Add tabbed panels to slides with panel set
  • Add a logo to all of your slides with a logo
  • Use the Tachyons CSS utility toolkit
  • Add a live video feed of your webcam
  • Add one-click code copying with a clipboard
  • Fit your slides to fill the browser window
  • Add extra CSS styles

Each item can be enabled separately, or load everything at once with a single call.

Try this demo and press the key “w”. Your webcam image will popup within the slides! (once you have enabled it in the browser, of course)

Summary, from where to start with?

  1. Take a look at this amazing online book: R Markdown cookbook. In there you have most of the information you need.
  2. To elaborate Slides, you can use the xaringan package, see xaringan package in action! Check also the author’s repository
  3. Check xaringanExtra repository for even more fancy slides (animation, panels within a slide, and much more). With xaringaExtra, you can even broadcast your slides and superpose your webcam image to them!

Final words

There are also other formats to show your work such as Microsoft® Word document. You can even work from templates to create a new Word file!

Finally, you can even use other languages in Rstudio to create your slides! See here for more info.

Let me know if you find this interesting as I did!

[1] R Markdown Cookbook, Yihui Xie, Christophe Dervieux, Emily Riederer, 2020

Let’s get in touch

I’m looking to be in touch with great people, contact me on my LinkedIn or my Twitter accounts

Originally published at https://community.dataquest.io on October 12, 2020.

Datascientist with background in electronics.