class: title

options(htmltools.dir.version = FALSE)
knitr::opts_chunk$set(
  fig.width = 10, fig.height = 6, fig.retina = 2,
  warning = FALSE, message = FALSE
)
js4shiny::html_setup(stylize = c("fonts", "variables", "code"))

10 Simple Rules

To Be an .code[#rstats] Ninja

.rstudio-conf-logo[]

.talk-meta[ .talk-author[ Garrick Aden-Buie ] .talk-date[ rstudio::conf(2020, "js4shiny", day = 1, session = 'am') ] ]


class: break bg-purple center middle

.white[xaringan]

.white[/ʃæ.'riŋ.ɡæn/]


class: center, middle, inverse, hide-count

.f1.header.text-shadow-1[ Get Started ]

.footnote[ This is a totally custom section break page. ]


Hello World

Install the xaringan package from Github:

devtools::install_github("yihui/xaringan")

--

You are recommended to use the RStudio IDE, but you do not have to.

--

--

.footnote[ [1] 中文用户请看这份教程

[2] See #2 if you do not see the template or addin in RStudio. ]


class: center, bottom, inverse

You only live once!


Hello Ninja

You are a presentation ninja! Yihui

As a presentation ninja, you certainly should not be satisfied by the "Hello World" example. You need to understand more about two things:

  1. The remark.js library;

  2. The xaringan package;

Basically xaringan injected the chakra of R Markdown (minus Pandoc) into remark.js. The slides are rendered by remark.js in the web browser, and the Markdown source needed by remark.js is generated from R Markdown (knitr).


remark.js

You can see an introduction of remark.js from its homepage. You should read the remark.js Wiki at least once to know how to

It is important to be familiar with remark.js before you can understand the options in xaringan.

.footnote[[*] It is different with Pandoc's Markdown! It is limited but should be enough for presentation purposes. Come on... You do not need a slide for the Table of Contents! Well, the Markdown support in remark.js may be improved in the future.]


class: break break-shiny

Using Shiny is Awesome

.fixed.bottom-1.right-2[]


class: break break-shiny huge

Shiny


xaringan

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.

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.


class: break break-javascript

Let's Do Some JavaScript


class: break break-javascript huge

.text-shadow-1[JavaScript]


JavaScript

```{js, js_live = FALSE} let x = 10 console.log(x * 20)

.footnote[
If you have `node` installed, this actually runs at compile time! 🎉
]

---

# JSON

```{json}
{"menu": {
  "id": "file",
  "value": "File",
  "popup": {
    "menuitem": [
      {"value": "New", "onclick": "CreateNewDoc()"},
      {"value": "Open", "onclick": "OpenDoc()"},
      {"value": "Close", "onclick": "CloseDoc()"}
    ]
  }
}}

remark.js vs xaringan

Some differences between using remark.js (left) and using xaringan (right):

.pull-left[ 1. Start with a boilerplate HTML file;

  1. Plain Markdown;

  2. Write JavaScript to autoplay slides;

  3. Manually configure MathJax;

  4. Highlight code with *;

  5. Edit Markdown source and refresh browser to see updated slides; ]

.pull-right[ 1. Start with an R Markdown document;

  1. R Markdown (can embed R/other code chunks);

  2. Provide an option autoplay;

  3. MathJax just works;*

  4. Highlight code with {{}};

  5. The RStudio addin "Infinite Moon Reader" automatically refreshes slides on changes; ]

.footnote[[*] Not really. See next page.]


R Code

# a boring regression
fit = lm(dist ~ 1 + speed, data = cars)
coef(summary(fit))
dojutsu = c('地爆天星', '天照', '加具土命', '神威', '須佐能乎', '無限月読')
grep('天', dojutsu, value = TRUE)

R Plots

par(mar = c(4, 4, 1, .1))
plot(cars, pch = 19, col = 'darkgray', las = 1)
abline(fit, lwd = 2)

class: header_background

Tables

If you want to generate a table, make sure it is in the HTML format (instead of Markdown or other formats), e.g.,

knitr::kable(head(iris), format = 'html')

Some Tips

--


Some Tips

--


Some Tips


Some Tips

.pull-left[ An example using a leading *:

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

Output:

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 ''````r
if (TRUE) {
*{{ message("Very important!") }}
}
```

Output:

if (TRUE) {
{{ message("Very important!") }}
}

It is valid R code so you can run it. Note that {{}} can wrap an R expression of multiple lines. ]


Some Tips

An example of using the trailing comment #<< to highlight lines:

`r ''````r
library(ggplot2)
ggplot(mtcars) + 
  aes(mpg, disp) + 
  geom_point() +   #<<
  geom_smooth()    #<<
```

Output:

library(ggplot2)
ggplot(mtcars) + 
  aes(mpg, disp) + 
  geom_point() +   #<<
  geom_smooth()    #<<

Some Tips


Macros


Macros (continued)



gadenbuie/js4shiny documentation built on March 25, 2024, 8:16 p.m.