7.6 Some tips

Lastly, we present a few tips that may help you make better presentations.

7.6.1 Autoplay slides

Slides can be automatically played if you set the autoplay option under nature (in milliseconds). For example, the next slide can be displayed automatically every 30 seconds in a lightning talk:

---
output:
  xaringan::moon_reader:
    nature:
      autoplay: 30000
---

7.6.2 Countdown timer

A countdown timer can be added to every page of the slides using the countdown option under nature. For example, if you want to spend one minute on every page when you give the talk, you can set:

---
output:
  xaringan::moon_reader:
    nature:
      countdown: 60000
---

Then you will see a timer counting down from 01:00, to 00:59, 00:58, … When the time is out, the timer will continue but the time turns red.

7.6.3 Highlight code lines

The option highlightLines: true of nature will highlight code lines that start with *, or are wrapped in {{ }}, or have trailing comments #<<:

---
output:
  xaringan::moon_reader:
    nature:
      highlightLines: true
---

Below are a few examples:

```r
if (TRUE) {
* message("Very important!")
}
```

```{r tidy=FALSE}
if (TRUE) {
{{ message("Very important!") }}
}
```

```{r tidy=FALSE}
library(ggplot2)
ggplot(mtcars) +
  aes(mpg, disp) +
  geom_point() +   #<<
  geom_smooth()    #<<
```

Note that the first way does not give you valid R code in the source document, but the latter two ways provide syntactically valid R code, and in the output slides, you will not see the tokens {{ }} or #<<. The lines will be highlighted with a yellow background by default.

7.6.4 Working offline

To make slides work offline, you need to download a copy of remark.js in advance, because xaringan uses the online version by default. You can use xaringan::summon_remark() to download the latest or a specified version of remark.js. By default, it is downloaded to libs/remark-latest.min.js.

Then change the chakra option in the YAML metadata to point to this file, e.g.,

output:
  xaringan::moon_reader:
    chakra: libs/remark-latest.min.js

Making the slides work offline can be tricky, since you may have other dependencies. The remark.js dependency is easy to deal with because it is a single JavaScript file; other dependencies such as MathJax can be extremely tricky. If you used Google web fonts in slides (the default theme uses Yanone Kaffeesatz, Droid Serif, and Source Code Pro), they will not work offline unless you download or install them locally. The Heroku app google-webfonts-helper can help you download fonts and generate the necessary CSS.

7.6.5 Macros

The Markdown syntax of remark.js can be amazingly extensible, because it allows users to define custom macros (JavaScript functions) that can be applied to Markdown text using the syntax ![:macroName arg1, arg2, ...] or ![:macroName arg1, arg2](this). For example, you can define a macro named scale to set the width of an image:

remark.macros.scale = function(w) {
  var url = this;
  return '<img src="' + url + '" style="width: ' + w + '" />';
};

Then the Markdown text

![:scale 50%](image.jpg)

will be translated to:

<img src="image.jpg" style="width: 50%" />

Now you should see that you can use cleaner pseudo-Markdown syntax to generate HTML.

To insert macros in xaringan slides, you can save your macros in a file (e.g., macros.js), and use the option beforeInit under the option nature, e.g.,

output:
  xaringan::moon_reader:
    nature:
      beforeInit: "macros.js"

The beforeInit option can be used to insert arbitrary JavaScript code before remark.js initializes the slides. Inserting macros is just one of its possible applications. For example, when you embed tweets from Twitter in slides, usually you need to load https://platform.twitter.com/widgets.js, which can be loaded via the beforeInit option.

7.6.6 Disadvantages

The xaringan package was originally designed for “ninja”, meaning that if you know CSS, you will be able to freely customize the style, otherwise you can only accept the default themes. Playing with CSS can be fun and rewarding, but it can also easily waste your time. Your aesthetic standards and taste may change from time to time, and you could end up tweaking the styles all the time.

The HTML output file generated from xaringan is not self-contained by default, as we mentioned in Section 7.4. The self-contained mode of xaringan may not work perfectly.

HTML widgets may not work well in xaringan. This might be improved in the future, but it is a little tricky technically.

When printing the slides to PDF from Google Chrome (see Section 4.1.10), I recommend that you open the slides and go through all pages at least once, to make sure all content has been rendered in the browser. Without navigating through all slides manually once, some content may not be printed correctly (such as MathJax expressions and HTML widgets).