Show your insights in R Markdown as slides!

campaign-creators-gMsnXqILjp4-unsplash

Introduction to R Markdown output formats

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

The main idea is to render output from a R Markdown file:

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

It’s important to know about react.js but it’s even more important to know about R Markdown syntax, specially about code chunks. Let’s see how your document should be organized and how to produce output from R code:

Header (YAML metadata)

The YAML metadata can influence the final document in many different ways. It is read by each of Pandoc, rmarkdown, and knitr. Along the way, the information that it contains can affect the code, content, and the rendering process.

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

This is quite similar to the typical Markdown structure:

# 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

A typical code chunk looks like this:

```{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

A body will typically look like this:

# 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

Slides from a R Markdown file are possible using HTML output.

“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 smart phones 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?

There is an extension for xaringa called xaringaExtra.
xaringanExtra is a playground of enhancements and extensions for xaringan slides:

  • Add an overview of your presentation with 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 panelset
  • Add a logo to all of your slides with logo
  • Use the Tachyons CSS utility toolkit
  • Add a live video feed of your webcam
  • Add one-click code copying with 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 enable it in the browser, of course)

Summary, from where to start with?

  1. Read the react.js wiki, where there is detailed documentation about how to format slides and use the presentation with keyboard shortcuts
  2. Take a look at this amazing online book: R Markdown cookbook. In there you have most of the information you need.
  3. To elaborate Slides, you can use the xaringan package, see xaringan package in action! Check also the author’s repository
  4. 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

Finding insights from your data is important but showing them accordingly is a key point. R Studio offers a markdown version for R which brings the possibility to show code output in your markdown files. This includes formulas and plots. In this article, a brief introduction is made to the next level of R Markdown: slides.

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 :smiley:

References

[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 to my LinkedIn or my twitter accounts :wave:t3:

9 Likes