class: center, middle, inverse, title-slide # Making cool HTML slides in R ## with the xaringan package ### ### 2021-12-03 --- # Introduction These slides provide an overview of how to use the `xaringan` package to create neat HTML slides in R. To begin, press `h` to toggle between the slide you are currently viewing, and a help slide detailing keyboard shortcuts to use in `xaringan` slides. --- ## Installation To install `xaringan` you can use either CRAN or Github (I used Github, I'm not sure if there's any major difference - the CRAN version might be more stable). ```r install.packages('xaringan') #or remotes::install_github('yihui/xaringan') ``` ## New xaringan file You can create a new set of `xaringan` slides in RStudio via <br> `File -> New File -> R Markdown -> From Template -> Ninja Presentation` --- # xaringan pros and cons Because `xaringan` uses `remark.js` (an in-browser markdown slideshow tool) instead of `pandoc`, there are some neat benefits, and one or two downsides. -- ### Main Pros * `anicon` package works smoothly * Syntax highlighting * Presenter mode * Slide scaling on different devices and resolutions * Touch support for smart devices -- ### Main Cons * The `pandoc` markdown converter doesn't work with `xaringan`, so you can't make citations using e.g. @citation * Slides are not standalone HTML files with no external dependencies - in other words, none of the external dependencies like images will be embedded in the HTML output file by default --- # Infinite Moon Reader `xaringan` has a handy live-preview RStudio add-in, `infinite moon reader`, which lets you preview your slides. You can do this as follows: -- * In RStudio, open your slides RMarkdown file. -- * In the console, run the command `inf_mr()` <br> (short for `infinite_moon_reader()`) -- * A preview of your slides should show up in the `Viewer` panel. <br> *It's worth noting, if you navigate to different sections of the file, the preview will change to that slide too!* -- * Now, any changes you make to this file should show up in the preview slides, in real time (maybe with a second delay). -- * If you close the preview for whatever reason, saving your file will reopen the (updated) preview. -- *Note: `infinite moon reader` doesn't automatically work if you are updating details on the title page. But simply save the file and it should update.* --- # Infinite Moon Reader suggested usage For me, the easiest option seems to be: * Open the slides file, * Run `inf_mr()`, * Click `Show in new window` in the `Viewer` section, so that a (larger) preview pops up in my browser, * Remove the preview from the `Viewer` section (can be a bit distracting). The slides in the browser should update automatically. If for whatever reason this stops working, I just save the file and the browser preview updates (sometimes adding code makes it stop auto-updating I have found). --- # Making slides in xaringan We use three dashes, i.e. `---` to specify the beginning of a new slide (no other characters or white spaces are allowed after the dashes). This must be preceded and followed by an empty line. We can use hashes to denote larger text: # 1 hash for a title, ## 2 hashes for a subtitle ### etc. --- # Helpful additions for teaching ## Presenter Mode A presenter mode is available to use in `xaringan` slides - these allow you to add details to slides that are only visible to you. The main two things to include are: * Notes with additional information. * A preview of the next slide, so you know what's up next. You can add slide notes to a slide using `???`, similar to how we make a new slide. These notes won't be immediately visible - press `p` to see them. ??? Yes there are some notes on this slide! As you can see, it's not just a simple pop-up, but rather quite a nicely laid out, intuitive display. I would have loved using this instead of beamer slides. You can press `p` again to close the Presenter mode. --- ## Presenter mode details To ensure the presenter details don't distract the audience, you can clone your slideshow view by pressing `c`. This opens a cloned view of the current slideshow in a new browser window, which keeps up with your presenter version (your browser might prevent pop-ups - mine did, so watch out for that). I think this would work well with Zoom, but it would be good to test it out. --- ## Animated Icons
Success!
I still don't understand why (since the identical code in `ioslides` or `slidy` slides fails), but the `anicon` package works well in `xaringan` slides (except for headings). ```r anicon::faa("leaf") ```
<i class="fas fa-leaf faa-flash animated "></i>
```r anicon::faa("leaf", size=2, bgcolour="MistyRose", colour="green") ```
<i class="fas fa-leaf fa-2x faa-flash animated " style=" color:green; background:MistyRose;"></i>
<img src="LTU_logo.jpg" style="height:2em; width:auto; "/> We can use our own images too
--- ## Highlighting R Code - setup With `xaringan` slides, we can highlight specific lines of R code, as desired. -- We set this up by adding the line `highlightLines: true` in the YAML section, i.e.: ```r --- output: xaringan::moon_reader: nature: highlightLines: true --- ``` --- ## Highlighting R Code - application Then for any line of code you want highlighted: * include a * at the start of the line, -- * or include a trailing comment #<< <br>(this works even after comments - see code example below) -- * or wrap the whole line in {{}} -- Personally I would go with option 2 or 3, since the first option doesn't give you valid R code in the source document. Option 1 is an artifact from remark.js. -- ## Example ```r *install.packages(palmerpenguins) # missing quotation marks! install.packages("palmerpenguins") ``` *Note: Sometimes the infinite moon reader won't immediately work with stuff like this - you may need to refresh/save a few times before highlights appear.* --- ## Highlighting R code - final notes you can also specify highlighting for specific rows in the code chunk specifications, e.g. by using the argument `highlight.output=c(1, 3)` we obtain: ```r head(penguins) ``` ``` *## # A tibble: 6 x 8 ## species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex year *## <fct> <fct> <dbl> <dbl> <int> <int> <fct> <int> ## 1 Adelie Torgersen 39.1 18.7 181 3750 male 2007 ## 2 Adelie Torgersen 39.5 17.4 186 3800 female 2007 ## 3 Adelie Torgersen 40.3 18 195 3250 female 2007 ## 4 Adelie Torgersen NA NA NA NA <NA> 2007 ## 5 Adelie Torgersen 36.7 19.3 193 3450 female 2007 ## 6 Adelie Torgersen 39.3 20.6 190 3650 male 2007 ``` The argument `highlight.output = TRUE` will highlight all output, and you can use `highlight.output=c(TRUE, FALSE)` to highlight every odd row, etc. I think we can change the highlight colour, by changing the CSS theme file. --- ## Incremental slides As you will have noticed, -- some of the preceding slides had content that appeared incrementally. This is very easy to set up - -- we just add a line with two dashes, i.e. `--`, wherever we want content to pause on a slide. -- This line doesn't have to be preceded and followed by a blank line (if it is the new content is placed on a new line). -- [Yihue Xie has a whole slide presentation on this, if you would like to check it out.](https://slides.yihui.org/xaringan/incremental.html#1) -- We can also build incremental slides using * layout slides * template slides There are some slides about these options later. --- ## Countdown timers We can add a countdown timer to slides, which I think could potentially be useful if we wanted to run snap tests for students during a presentation. We do this by adding the argument `countdown:` under the `nature:` element in the YAML section. The number we specify is the countdown time in milliseconds - e.g. here we have specified 1 minute: ```r --- output: xaringan::moon_reader: nature: countdown: 60000 --- ``` The timer resets whenever you move to a new slide. If you stay on a slide for longer than the timer limit, the count turns red. Unfortunately, I can't find an option to include the timer for specific slides only. --- ## Autoplay If you know that you need your content to be presented within a set interval of time, you could make your slides autoplay, by adding the argument `autoplay` under the `nature:` element in the YAML section (like for `countdown`). The number specified is the time spent on each slide (in milliseconds) before moving on to the next slide. E.g. here we have specified 30 seconds per slide, and for the slides to loop once finished. ```r --- output: xaringan::moon_reader: nature: autoplay: 30000 loop: true # to loop once the last slide is reached --- ``` --- ## Maths LaTeX maths works easily in `xaringan`, e.g. `\(x=1\)` using `$` or `$$x=1$$` using `$$`. --- # Properties in xaringan ## class You can add individual properties, like `class`, `name` and `background-image`, to each slide (do so before adding content for the slide). We can use the `class` property to align content, e.g. ```r # horizontal alignment options class: right class: left class: center # vertical alignment options class: top class: middle class: bottom ``` *Note these have to be placed at the start of a slide to take effect.* --- layout: true class: inverse # Layout Slides Using the `layout` and `class` properties, we can also change the presentation of a sub-series of slides. The text shown here in this line and above is included in our base `layout` template, and will appear on all `layout` slides. --- ## Overview A layout slide (with the slide property `layout: true` added before any slide text) is good for a series of slides that share some common elements, such as the top-level heading. Here we have set the class to be `inverse`. --- ## They work a little differently to other slides Inside the `layout` slide environment, using `---` doesn't start a completely new slide, but rather leads to a new slide that inherits the layout properties of the first slide for which you have used the `layout: true` property. --- So for example, here the `## Layout Slides` title is shown, along with the text included in the first `layout: true` slide, even though it's not included in the Markdown code for this specific slide. --- You can also override or add more properties within `layout` slides, e.g. `background-image` - more on this soon. --- Until you turn off the layout setting, it will continue to be used for subsequent slides. --- layout: false class: middle By including `layout: false` in the slide properties, the layout slide setting will stop being used. --- # Template slides Using `--` and `layout: true` to create incremental slides should be flexible enough to be sufficient for most contexts. If you want even more control over a section of slides, you can use `template` slides. To create a template, we specify the desired properties at the start of a slide. These properties must include `name`, so that you can refer to this template on another slide. Just like for `layout` slides, when you are specifying all the details for a new `template`, this should be done in a separate slide - anything included in the 'template creation' slide will be inherited by any slides using the template. --- name: penguin class: top background-image: url("https://live.staticflickr.com/7476/15977828971_3658c9aa83_b.jpg") background-size: contain This slide has a template with the name `penguin`, and also has several other properties specified. --- template: penguin If I include the property `template: penguin` on another slide, the penguin template is used for the slide. Please note, you can't use this template on a slide before the slide in which you defined the template. You can however overwrite a template, so that's something to watch out for if using multiple templates in the one set of slides. --- name: template-placeholder ## Placeholders in template slides {{content}} There is also the option to embed the contents of a new slide in a particular position within a template slide using a `placeholder` template. For this to work, we have our usual slide content, and then we place the code `\(\{\{content\}\}\)` in the section of the slide where we would like new content to appear in subsequent slides. This slide uses a `placeholder` template, and has the property `name: template-placeholder` specified. The `\(\{\{content\}\}\)` code has been placed between the heading on this slide, and the text. --- template: template-placeholder Here is an example of how this `placeholder` template works. As you can see, this text is appearing in the position specified via the inclusion of the code `\(\{\{content\}\}\)` in the initial `placeholder` template slide. I'm not sure how useful this option is, but I figured it was worth covering. --- # class options for segmenting slides We can style slide elements using CSS - the built-in classes offer a few ways to segment slides into columns. .pull-left[ 1. Using `.pull-left[]` 2. We can have details on the left ] .pull-right[ 1. And `.pull-right[]`, 2. And then more details on the right 3. With the same 4. Or a different number of rows 5. These two columns are equal width ] --- ## Three columns .left[We can also use `.left[]`,] .center[`.center[]` and] .right[`.right[]` as shown here] .left[It's worth noting] .center[that if we have] .right[too many words in a column specified this way, then the columns can overlap, which is a shame] --- ## Sidebars .left-column[ ### Side bar example If you use multiple headings here, only the first final heading will be black ### Previous headings will be grey ] .right-column[ We can also include sidebars in our slides, using <br> `.left-column` and `.right-column`. By default the left sidebar is narrow (20% of the slide width) ] --- class: middle Using the `class` property, you can also assign specific `themes` to slides, via CSS. This means you will need a separate CSS file, which you call in the YAML output section - indented in the line below `xaringan::moon_reader:`. ```r e.g. --- output: xaringan::moon_reader: css: ["default", "extra.css"] --- ``` --- # Themes in xaringan If we want to change the default theme, we can include a custom-made CSS file. Alternatively, rather than making our own CSS file from scratch, we could use, or modify, one of the existing available themes. To check available themes, we run the R code: ```r names(xaringan:::list_css()) ``` -- To use one of the available themes, we specify the `css` option in the YAML section, e.g.: ```r --- output: xaringan::moon_reader: css: [default, metropolis, metropolis-fonts] --- ``` --- ## List of Themes available ``` ## [1] "chocolate-fonts" "chocolate" "default-fonts" "default" "duke-blue" "fc-fonts" "fc" ## [8] "glasgow_template" "hygge-duke" "hygge" "ki-fonts" "ki" "kunoichi" "lucy-fonts" ## [15] "lucy" "metropolis-fonts" "metropolis" "middlebury-fonts" "middlebury" "nhsr-fonts" "nhsr" ## [22] "ninjutsu" "rladies-fonts" "rladies" "robot-fonts" "robot" "rutgers-fonts" "rutgers" ## [29] "shinobi" "tamu-fonts" "tamu" "uio-fonts" "uio" "uo-fonts" "uo" ## [36] "uol-fonts" "uol" "useR-fonts" "useR" "uwm-fonts" "uwm" ``` *Note: Some separate documents will be made to show the different available themes.* --- background-image: url("https://live.staticflickr.com/7563/16054445089_d1f4f77853_b.jpg") background-position: center background-size: contain # Adding background images Here I have used the CSS property **background-image** to add an image from the Internet. I can also use the related properties **background-position** and **background-size** to adjust the image. --- class: middle background-image: url(LTU_logo.jpg) background-position: top background-size: 50% We can also add local images. * `background-position` takes arguments similar to `class` - i.e. `left`, `right`, `top`, `center` and `middle`. * `background-size` options include `contain` (to stretch or condense the image to fit the whole slide) or specific numbers to scale the image, e.g. `background-size: 15%`. --- ## Videos As well as images, you can also include YouTube videos: * Navigate to a YouTube video * Click the `Share` button under the video * Select the `embed` option * Copy and paste the code <iframe width="560" height="315" src="https://www.youtube.com/embed/6JMxHRpr7f8" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> --- template:penguin #### Just a reminder that once a template is defined, you can include a template slide wherever you want later on in your slides. This could be handy if we wanted to have a template for quizzes, for example. --- # Interactive Content We can use packages like `plotly`, `leaflet` and `DT` to include interactive content in our slides - e.g.:
--- ## Interactive tables Interactive tables (created using the `DT` (DataTables) R package) might be of more immediate applicability - e.g.:
--- It's worth noting that in general, if you want to generate a table, it should be in HTML format (instead of Markdown or other formats), e.g., ```r knitr::kable(head(penguins), format = 'html') ``` <table> <thead> <tr> <th style="text-align:left;"> species </th> <th style="text-align:left;"> island </th> <th style="text-align:right;"> bill_length_mm </th> <th style="text-align:right;"> bill_depth_mm </th> <th style="text-align:right;"> flipper_length_mm </th> <th style="text-align:right;"> body_mass_g </th> <th style="text-align:left;"> sex </th> <th style="text-align:right;"> year </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Adelie </td> <td style="text-align:left;"> Torgersen </td> <td style="text-align:right;"> 39.1 </td> <td style="text-align:right;"> 18.7 </td> <td style="text-align:right;"> 181 </td> <td style="text-align:right;"> 3750 </td> <td style="text-align:left;"> male </td> <td style="text-align:right;"> 2007 </td> </tr> <tr> <td style="text-align:left;"> Adelie </td> <td style="text-align:left;"> Torgersen </td> <td style="text-align:right;"> 39.5 </td> <td style="text-align:right;"> 17.4 </td> <td style="text-align:right;"> 186 </td> <td style="text-align:right;"> 3800 </td> <td style="text-align:left;"> female </td> <td style="text-align:right;"> 2007 </td> </tr> <tr> <td style="text-align:left;"> Adelie </td> <td style="text-align:left;"> Torgersen </td> <td style="text-align:right;"> 40.3 </td> <td style="text-align:right;"> 18.0 </td> <td style="text-align:right;"> 195 </td> <td style="text-align:right;"> 3250 </td> <td style="text-align:left;"> female </td> <td style="text-align:right;"> 2007 </td> </tr> <tr> <td style="text-align:left;"> Adelie </td> <td style="text-align:left;"> Torgersen </td> <td style="text-align:right;"> NA </td> <td style="text-align:right;"> NA </td> <td style="text-align:right;"> NA </td> <td style="text-align:right;"> NA </td> <td style="text-align:left;"> NA </td> <td style="text-align:right;"> 2007 </td> </tr> <tr> <td style="text-align:left;"> Adelie </td> <td style="text-align:left;"> Torgersen </td> <td style="text-align:right;"> 36.7 </td> <td style="text-align:right;"> 19.3 </td> <td style="text-align:right;"> 193 </td> <td style="text-align:right;"> 3450 </td> <td style="text-align:left;"> female </td> <td style="text-align:right;"> 2007 </td> </tr> <tr> <td style="text-align:left;"> Adelie </td> <td style="text-align:left;"> Torgersen </td> <td style="text-align:right;"> 39.3 </td> <td style="text-align:right;"> 20.6 </td> <td style="text-align:right;"> 190 </td> <td style="text-align:right;"> 3650 </td> <td style="text-align:left;"> male </td> <td style="text-align:right;"> 2007 </td> </tr> </tbody> </table> --- ## Modifying the title slide I noted earlier that the title slide is created automatically, and is set to `class: center, middle, inverse, title-slide` by default. However we can modify the classes applied to the title slide with the `titleSlideClass` option of `nature` (`title-slide` is always applied), e.g.: ```r output: xaringan::moon_reader: nature: titleSlideClass: [top, left, inverse] ``` or remove it completely via the `seal` option. --- ## Self-contained mode I noted earlier that by default `xaringan` slides' html outputs aren't self-contained - but we can change this. To turn the self-contained mode on, use ```r output: xaringan::moon_reader: self_contained: true ``` Apparently this may or may not work. To improve the chances of it working, for images we include in the slides, we should use * `` * or `<img src="PATH" />` * or `background-image: url(PATH)` to be safe. --- # Working offline By default `xaringan` slides work best online. If working offline, you need to have a copy of `remark.js` downloaded. You can download the latest version using ```r xaringan::summon_remark() ``` It should be saved to `libs/remark-latest.min.js`. Then you need to change the details in the YAML section using the `chakra` element, e.g.: ```r output: xaringan::moon_reader: chakra: libs/remark-latest.min.js ``` --- template: penguin I hope this has provided a helpful overview of the `xaringan` package. --- class: inverse ## Extra links [remark overview](https://remarkjs.com/#1) [remark slide property options](https://github.com/gnab/remark/wiki/Markdown#slide-properties) [DT package](https://rstudio.github.io/DT/)