class: center, middle, inverse, title-slide # Presentation Ninja ## ⚔
with xaringan ### Anna Krystalli (channeling Yihui Xie) ### 2019-08-06 --- # 👋 Hello ### me: **Dr Anna Krystalli** - **Research Software Engineer**, _University of Sheffield_ + twitter **@annakrystalli** + github **@annakrystalli** + email **a.krystalli[at]sheffield.ac.uk** - **Editor [rOpenSci](http://onboarding.ropensci.org/)** - **Co-organiser:** [Sheffield R Users group](https://www.meetup.com/SheffieldR-Sheffield-R-Users-Group/) <br> ### slides: **http://bit.ly/xaringan-shef** --- class: center, middle, inverse # Background --- # xaringan .pull-left[ ### /ʃaː.'riŋ.ɡan/ The R package name **xaringan** was derived from **Sharingan**, a dōjutsu in the Japanese anime _Naruto_ with two abilities: - the "Eye of Insight" - the "Eye of Hypnotism" ] .pull-right[ ![:scale 200%](https://media1.tenor.com/images/31a1e8fc525526de04d785740a46db8e/tenor.gif?itemid=6062752) ] <br> > I think a **presentation is basically a way to communicate insights** to the audience, and a **great presentation may even "hypnotize"** the audience. <br> _Yihui Xie_ --- class: inverse, center, middle # Get Started --- # Hello World ### Install Install the **xaringan** package from [Github](https://github.com/yihui/xaringan): ```r devtools::install_github("yihui/xaringan") ``` -- ### Create new You are recommended to use the [RStudio IDE](https://www.rstudio.com/products/rstudio/), but you do not have to. - Create a new R Markdown document from the menu: **`File -> New File -> R Markdown -> From Template -> Ninja Presentation`** -- ### Render to html - **Click the `Knit`** button to compile it; - or **use the [RStudio Addin](https://rstudio.github.io/rstudioaddins/) "Infinite Moon Reader"** to live preview the slides + _every time you update and save the Rmd document, the slides will be automatically reloaded in RStudio Viewer._ --- background-image: url(https://github.com/yihui/xaringan/releases/download/v0.0.2/karl-moustache.jpg) background-position: 50% 50% class: center, bottom, inverse # You only live once! --- # Backend ### Xaringan package wraps the [remark.js](https://remarkjs.com) library + A simple, in-browser, markdown-driven slideshow tool. + You can find an introduction of remark.js from [its homepage](https://remarkjs.com). + Check the [Xaringan Wiki](https://github.com/yihui/xaringan/wiki) for more in depth info on customising your presentations --- # Xaringan features 1. Start with an **R Markdown document** + powered by markdown + can embed R/other code chunks 1. **MathJax support** for mathematical expressions 1. **Code highlighting**; 1. The **RStudio addin "Infinite Moon Reader"** to automatically refresh slides on changes; --- class: inverse, middle, center # Using xaringan --- # Presentation level settings Provides an R Markdown output format `xaringan::moon_reader` as a wrapper for remark.js, and you can use it in the YAML metadata, e.g. ```yaml --- title: "A Cool Presentation" output: xaringan::moon_reader: yolo: true nature: autoplay: 30000 --- ``` See the **help page `?xaringan::moon_reader` for all possible options** that you can use. --- # Selected settings ## Autoplay Slides can be automatically played if you set the `autoplay` option under `nature`, .small[e.g. go to the next slide every 30 seconds in a lightning talk:] ```yaml output: xaringan::moon_reader: nature: autoplay: 30000 ``` -- ## Countdown timer A countdown timer can be added to every page of the slides using the `countdown` option under `nature`, .small[e.g. if you want to spend one minute on every page when you give the talk, you can set:] ```yaml output: xaringan::moon_reader: nature: countdown: 60000 ``` .small[Then you will see a timer counting down from `01:00`, to `00:59`, `00:58`, ... ] --- # Title Slide The **title slide is created automatically by `xaringan`**, but it is just another remark.js slide added before your other slides. The title slide is **set to `class: center, middle, inverse, title-slide` by default**. You can change the classes applied to the title slide with the **`titleSlideClass` option of `nature`** (`title-slide` is always applied). ```yaml output: xaringan::moon_reader: nature: titleSlideClass: [top, left, inverse] ``` -- ### custom title slide - If you'd like to create your own title slide, disable **xaringan**'s title slide with the `seal = FALSE` option of `moon_reader`. ```yaml output: xaringan::moon_reader: seal: false ``` --- class: inverse, middle, center # Working with slides --- # Create a new slide Delimit slides **using three hyphens (`---`)**, eg: ```md --- # This is the title of the slide The body of the slide is in **markdown**. You can use markdown for: ## Subheadings (eg this h2) You can use it for lists: - _list item_ - _another list item_ And of course [hyperlinks](https://en.wikipedia.org/wiki/Hyperlink) --- ``` *** For more information on markdown see [R Markdown: The Definitive Guide](https://bookdown.org/yihui/rmarkdown/) or this tutorial or [Literate Programming in R](https://annakrystalli.me/literate-programming/) --- # This is the title of the slide The body of the slide is in **markdown**. You can use markdown for: ## Subheadings (eg this h2) You can use it for lists: - _list item_ - _another list item_ And of course [hyperlinks](https://en.wikipedia.org/wiki/Hyperlink) --- # Slide properties A slide **can have a few properties**, including **`class`** and **`background-image`**, etc. -- Properties are **specified in the beginning of a slide**, e.g. ```markdown --- class: inverse, middle, center # A Subsection slide --- ``` will create the following slide 👉 --- class: inverse, middle, center # A Subsection slide --- # Background images **Background images can be set via the `background-image` property**. The image can be either a local file or an online image. The **path should be put inside `url()`**, which is the CSS syntax. You can also **set the background image size and position**, e.g., ```yaml --- background-image: url("../assets/logos/xaringan-hex.png") background-position: center background-size: contain --- ``` --- background-image: url("../assets/logos/xaringan-hex.png") background-position: center background-size: contain --- background-image: url("https://media1.tenor.com/images/28ee6748d1e75ed138a58dfda27a0469/tenor.gif?itemid=4855087") background-position: center background-size: contain class: inverse # Works with gifs too! --- class: middle, center, inverse # Content classes ### You can assign classes to any elements on a slide, too. ## The syntax is `.className[content]`. --- # Positioning elements - There are a few built-in content classes, `.left[ ]`, `.center[ ]`, and `.right[ ]`, to align elements horizontally on a slide, e.g., ```markdown .center[![xaringan hex](../assets/logos/xaringan-hex.png)] ``` .center[![xaringan hex](../assets/logos/xaringan-hex.png)] --- # Sidebar layout `.left-column[ ]` and `.right-column[ ]` provide a sidebar layout. ```markdown .left-column[ ## Sidebar header Sidebar description ] ![xaringan hex](../assets/logos/xaringan-hex.png) ``` --- # Sidebar output .left-column[ ## Sidebar header Sidebar description ] ![xaringan hex](../assets/logos/xaringan-hex.png) --- # two-column layout `.pull-left[ ]` and `.pull-right[ ]` provide a two-column layout, and the two columns are of the same width. ```markdown .pull-left[ - One bullet. - Another bullet. ] .pull-right[ ![xaringan hex](../assets/logos/xaringan-hex.png) ] ``` --- # two-column: _output_ .pull-left[ - One bullet. - Another bullet. ] .pull-right[ ![an image](../assets/logos/xaringan-hex.png) ] --- # Incremental slide content The easiest way to **define incremental splits is by using `--`** -- ```md - Incremental point 1 -- - Incremental point 2 ``` *** -- - Incremental point 1 -- - Incremental point 2 -- *** For more options see [this presentation](https://slides.yihui.name/xaringan/incremental.html). --- --- # Math Expressions You can write LaTeX math expressions inside dollar signs ## Display You can use the display style with double dollar signs: ``` $$\bar{X}=\frac{1}{n}\sum_{i=1}^nX_i$$ ``` `$$\bar{X}=\frac{1}{n}\sum_{i=1}^nX_i$$` ## Inline You can use the inline style with single dollar signs: ``` Where $\bar{X}$ is the mean of the observations $X$. ``` Where `\(\bar{X}\)` is the mean of the observations `\(X\)`. --- # R Code Print the results of statistical tests ```r fit = lm(dist ~ 1 + speed, data = cars) coef(summary(fit)) ``` ``` # Estimate Std. Error t value Pr(>|t|) # (Intercept) -17.579095 6.7584402 -2.601058 1.231882e-02 # speed 3.932409 0.4155128 9.463990 1.489836e-12 ``` --- # R Code Print tibbles ```r gapminder::gapminder ``` ``` ## # A tibble: 1,704 x 6 ## country continent year lifeExp pop gdpPercap ## <fct> <fct> <int> <dbl> <int> <dbl> ## 1 Afghanistan Asia 1952 28.8 8425333 779. ## 2 Afghanistan Asia 1957 30.3 9240934 821. ## 3 Afghanistan Asia 1962 32.0 10267083 853. ## 4 Afghanistan Asia 1967 34.0 11537966 836. ## 5 Afghanistan Asia 1972 36.1 13079460 740. ## 6 Afghanistan Asia 1977 38.4 14880372 786. ## 7 Afghanistan Asia 1982 39.9 12881816 978. ## 8 Afghanistan Asia 1987 40.8 13867957 852. ## 9 Afghanistan Asia 1992 41.7 16317921 649. ## 10 Afghanistan Asia 1997 41.8 22227415 635. ## # … with 1,694 more rows ``` --- # R Plots Easy to include plots in your presentations. Can configure the output using chunk options .small[(e.g. `fig.width`/`fig.height`, hide/show code with `echo=T/F`, `dev='svg'` etc)]. ```r library(ggplot2) p <- ggplot(gapminder::gapminder, aes(gdpPercap, lifeExp, color = continent)) + geom_point(alpha = 0.5) + geom_smooth() + scale_x_log10() + theme_bw() p ``` ![](xaringan_files/figure-html/unnamed-chunk-4-1.svg)<!-- --> --- # Tables If you want to generate a table, make sure it is in the HTML format (instead of Markdown or other formats), e.g., ```r knitr::kable(head(iris), format = 'html') ``` <table> <thead> <tr> <th style="text-align:right;"> Sepal.Length </th> <th style="text-align:right;"> Sepal.Width </th> <th style="text-align:right;"> Petal.Length </th> <th style="text-align:right;"> Petal.Width </th> <th style="text-align:left;"> Species </th> </tr> </thead> <tbody> <tr> <td style="text-align:right;"> 5.1 </td> <td style="text-align:right;"> 3.5 </td> <td style="text-align:right;"> 1.4 </td> <td style="text-align:right;"> 0.2 </td> <td style="text-align:left;"> setosa </td> </tr> <tr> <td style="text-align:right;"> 4.9 </td> <td style="text-align:right;"> 3.0 </td> <td style="text-align:right;"> 1.4 </td> <td style="text-align:right;"> 0.2 </td> <td style="text-align:left;"> setosa </td> </tr> <tr> <td style="text-align:right;"> 4.7 </td> <td style="text-align:right;"> 3.2 </td> <td style="text-align:right;"> 1.3 </td> <td style="text-align:right;"> 0.2 </td> <td style="text-align:left;"> setosa </td> </tr> <tr> <td style="text-align:right;"> 4.6 </td> <td style="text-align:right;"> 3.1 </td> <td style="text-align:right;"> 1.5 </td> <td style="text-align:right;"> 0.2 </td> <td style="text-align:left;"> setosa </td> </tr> <tr> <td style="text-align:right;"> 5.0 </td> <td style="text-align:right;"> 3.6 </td> <td style="text-align:right;"> 1.4 </td> <td style="text-align:right;"> 0.2 </td> <td style="text-align:left;"> setosa </td> </tr> <tr> <td style="text-align:right;"> 5.4 </td> <td style="text-align:right;"> 3.9 </td> <td style="text-align:right;"> 1.7 </td> <td style="text-align:right;"> 0.4 </td> <td style="text-align:left;"> setosa </td> </tr> </tbody> </table> --- class: inverse, center, middle # HTML Widgets --- # Leaflet maps ```r library(leaflet) leaflet() %>% addTiles() %>% setView(-1.479538, 53.380851, zoom = 17) ```
--- # Interactive data tables ```r DT::datatable( head(gapminder::gapminder, 10), fillContainer = FALSE, options = list(pageLength = 8) ) ```
--- ## Interactive R plots ```r plotly::ggplotly(p) ```
--- ## Animated R plots ```r p <- ggplot(gapminder::gapminder, aes(gdpPercap, lifeExp, size = pop, color = continent, frame = year)) + geom_point() + scale_x_log10() + theme_bw() ``` ```r plotly::ggplotly(p) ```
--- # Highlight Code Xaringan supports highlighting of code. **Enable using option `highlightLines: true` in `nature`** ```yaml output: xaringan::moon_reader: nature: highlightLines: true ``` Once enabled, code lines that: - start with **`*`**, - are wrapped in **`{{ }}`** - have trailing comments **`#<<`** will be highlighted --- # Highlight Code: `*` or `{{}}` .pull-left[ An example using a leading `*`: ```r if (TRUE) { ** message("Very important!") } ``` Output: ```r if (TRUE) { * message("Very important!") } ``` **This is invalid R code**, so it is a plain fenced code block that is not executed. ] .pull-right[ An example using `{{}}`: ```` ```{r tidy=FALSE} if (TRUE) { *{{ message("Very important!") }} } ``` ```` Output: ```r if (TRUE) { * message("Very important!") } ``` ``` ## Very important! ``` **This is valid R code** so you can run it. Note that `{{}}` can wrap an R expression of multiple lines. ] --- # Highlight Code: _trailing comment_ An example of using the trailing comment `#<<` to highlight lines: ````markdown ```{r tidy=FALSE} library(ggplot2) ggplot(mtcars) + aes(mpg, disp) + geom_point() + #<< geom_smooth() #<< ``` ```` Output: ```r library(ggplot2) ggplot(mtcars) + aes(mpg, disp) + * geom_point() + * geom_smooth() ``` --- # Highlight Output: _chunk options_ You can also use the **chunk option `highlight.output`** to highlight specific lines of the **text output** from a code chunk. eg: - `highlight.output = TRUE` ➡️ highlight all lines - `highlight.output = c(1, 3)` ➡️ highlight the first and third line. ````md ```{r, highlight.output=c(1, 3)} head(iris) ``` ```` ``` *## # A tibble: 6 x 6 ## country continent year lifeExp pop gdpPercap *## <fct> <fct> <int> <dbl> <int> <dbl> ## 1 Afghanistan Asia 1952 28.8 8425333 779. ## 2 Afghanistan Asia 1957 30.3 9240934 821. ## 3 Afghanistan Asia 1962 32.0 10267083 853. ## 4 Afghanistan Asia 1967 34.0 11537966 836. ## 5 Afghanistan Asia 1972 36.1 13079460 740. ## 6 Afghanistan Asia 1977 38.4 14880372 786. ``` --- # Spot quiz: What will `highlight.output = c(TRUE, FALSE)` do? (Hint: think about R's recycling of vectors) -- ``` *## # A tibble: 6 x 6 ## country continent year lifeExp pop gdpPercap *## <fct> <fct> <int> <dbl> <int> <dbl> ## 1 Afghanistan Asia 1952 28.8 8425333 779. *## 2 Afghanistan Asia 1957 30.3 9240934 821. ## 3 Afghanistan Asia 1962 32.0 10267083 853. *## 4 Afghanistan Asia 1967 34.0 11537966 836. ## 5 Afghanistan Asia 1972 36.1 13079460 740. *## 6 Afghanistan Asia 1977 38.4 14880372 786. ``` --- # Emojis ```r devtools::install_github("hadley/emo") ``` ```r emo::ji_find("sparkle") ``` ``` ## # A tibble: 4 x 2 ## name emoji ## <chr> <chr> ## 1 sparkling_heart 💖 ## 2 star2 🌟 ## 3 sparkler 🎇 ## 4 sparkles ✨ ``` .pull-left[ ```r emo::ji("sparkles") ``` ``` ## ✨ ``` ] .pull-right[add sparkles with emojis! ✨] --- # Some Tips Do not forget to try the `yolo` option of `xaringan::moon_reader`. ```yaml output: xaringan::moon_reader: yolo: true ``` .pull-left[ Randomly places a slide with a photo of [Karl Broman]() in the presentation 😄 ] .pull-right[ ![:scale 400px](https://github.com/yihui/xaringan/releases/download/v0.0.2/karl-moustache.jpg) ] --- # CSS Among all options in `xaringan::moon_reader`, the most challenging but perhaps also the most rewarding one is **`css`, because it allows you to customize the appearance of your slides** using any CSS rules or hacks you know. You can see the default CSS file [here](https://github.com/yihui/xaringan/blob/master/inst/rmarkdown/templates/xaringan/resources/default.css). You can completely replace it with your own CSS files, or define new rules to override the default. See the help page `?xaringan::moon_reader` for more information. --- # CSS For example, suppose you **want to change the font for code to "Ubuntu Mono"**. ### Create CSS file `ubuntu-mono.css`: ```css @import url(https://fonts.googleapis.com/css?family=Ubuntu+Mono:400,700,400italic); .remark-code, .remark-inline-code { font-family: 'Ubuntu Mono'; } ``` ### Set css in YAML ```yaml output: xaringan::moon_reader: css: ["default", "ubuntu-mono.css"] ``` Here I assume `ubuntu-mono.css` is under the same directory as your Rmd. --- # Themes Don't want to learn CSS? Okay, you can use some user-contributed themes. A theme typically consists of two CSS files: - `foo.css` and - `foo-fonts.css`, where `foo` is the theme name. ### Existing themes: ```r names(xaringan:::list_css()) ``` ``` ## [1] "chocolate-fonts" "chocolate" "default-fonts" ## [4] "default" "duke-blue" "fc-fonts" ## [7] "fc" "hygge-duke" "hygge" ## [10] "kunoichi" "lucy-fonts" "lucy" ## [13] "metropolis-fonts" "metropolis" "middlebury-fonts" ## [16] "middlebury" "ninjutsu" "rladies-fonts" ## [19] "rladies" "robot-fonts" "robot" ## [22] "rutgers-fonts" "rutgers" "shinobi" ## [25] "tamu-fonts" "tamu" "uo-fonts" ## [28] "uo" "uol-fonts" "uol" ``` --- # Themes To use a theme, you can specify the `css` option as an array of CSS filenames (without the `.css` extensions), e.g., ```yaml output: xaringan::moon_reader: css: [default, metropolis, metropolis-fonts] ``` If you want to contribute a theme to **xaringan**, please read [this blog post](https://yihui.name/en/2017/10/xaringan-themes). --- background-image: url("https://github.com/emitanaka/ninja-theme/raw/master/docs/themes/kunoichi/images/kunoichi-showcase.gif") background-size: contain background-position: bottom # Emi Tanaka's **Kunoichi** _(female Ninja)_ Theme [](https://emitanaka.github.io/ninja-theme) --- # Use Kinoichi ```yaml output: xaringan::moon_reader: css: ["kunoichi", "ninjutsu"] ``` ## Featured Ninja and their Ninja Slides * [Sarah Romanes](https://twitter.com/sarah_romanes) - [Machine Learning 101](http://bit.ly/rladies-sydney-ML-1) ([source files](https://github.com/sarahromanes/r-ladies-ML-1)) * [William Chase](https://twitter.com/W_R_Chase) - [What I Wish I Knew When I Started R](https://www.williamrchase.com/slides/intro_r_anthropology_2018) ([source files](https://github.com/will-r-chase/blog/tree/master/static/slides)) * [Eric Nantz](https://twitter.com/thercast) - [The officer package - making PowerPoint slides from R](https://rpodcast.github.io/officer-advrmarkdown) ([source_files](https://github.com/rpodcast/officer-advrmarkdown)) --- # Some Tips: 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 (see the help page `?xaringan::moon_reader`). - 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 YAML to point to this file, e.g. ```yaml output: xaringan::moon_reader: chakra: libs/remark-latest.min.js ``` - If you used Google fonts in slides (the default theme uses _Yanone Kaffeesatz_, _Droid Serif_, and _Source Code Pro_), they won't work offline unless you download or install them locally. The Heroku app [google-webfonts-helper](https://google-webfonts-helper.herokuapp.com/fonts) can help you download fonts and generate the necessary CSS. --- class: inverse, middle, center background-image: url(https://upload.wikimedia.org/wikipedia/commons/3/39/Naruto_Shiki_Fujin.svg) background-size: contain # Naruto --- # Naruto terminology The **xaringan** package borrowed a few terms from Naruto, such as - [Sharingan](http://naruto.wikia.com/wiki/Sharingan) (写輪眼; the package name) - The [moon reader](http://naruto.wikia.com/wiki/Moon_Reader) (月読; an attractive R Markdown output format) - [Chakra](http://naruto.wikia.com/wiki/Chakra) (查克拉; the path to the remark.js library, which is the power to drive the presentation) - [Nature transformation](http://naruto.wikia.com/wiki/Nature_Transformation) (性質変化; transform the chakra by setting different options) - The [infinite moon reader](http://naruto.wikia.com/wiki/Infinite_Tsukuyomi) (無限月読; start a local web server to continuously serve your slides) - The [summoning technique](http://naruto.wikia.com/wiki/Summoning_Technique) (download remark.js from the web) You can click the links to know more about them if you want. The jutsu "Moon Reader" may seem a little evil, but that does not mean your slides are evil. --- # Further Reading: - [Chapter 7 xaringan Presentations](https://bookdown.org/yihui/rmarkdown/xaringan.html) --- class: center, middle # Thanks! Slides created via the R package [**xaringan**](https://github.com/yihui/xaringan). The chakra comes from [remark.js](https://remarkjs.com), [**knitr**](http://yihui.name/knitr), and [R Markdown](https://rmarkdown.rstudio.com).