christophe dervieux | 44a2037 | 2021-09-17 10:35:30 +0200 | [diff] [blame] | 1 | --- |
| 2 | title: "Rmarkdown Presentation including Plotly" |
| 3 | output: |
| 4 | revealjs::revealjs_presentation |
| 5 | --- |
| 6 | |
| 7 | # Setup |
| 8 | |
| 9 | ```{r, message=FALSE} |
| 10 | knitr::opts_chunk$set(echo = FALSE) |
| 11 | knitr::opts_template$set(demo = list(echo = TRUE, eval = FALSE)) |
| 12 | xfun::pkg_attach2("plotly") |
| 13 | xfun::pkg_load2("ggplot2") |
| 14 | data(economics, package = "ggplot2") |
| 15 | ``` |
| 16 | |
| 17 | |
| 18 | # Simple example |
| 19 | |
| 20 | ```{r simple} |
| 21 | set.seed(99) |
| 22 | plot_ly() %>% |
| 23 | add_trace( |
| 24 | type = "scatter", |
| 25 | mode = "markers+lines+text", |
| 26 | x = 4:6, |
| 27 | y = 4:6, |
| 28 | text = replicate(3, praise::praise("You are ${adjective}! 🙌")), |
| 29 | textposition = "right", |
| 30 | hoverinfo = "text", |
| 31 | textfont = list(family = "Roboto Condensed", size = 16) |
| 32 | ) %>% |
| 33 | layout(xaxis = list(range = c(3, 8))) |
| 34 | ``` |
| 35 | |
| 36 | ## Code |
| 37 | |
| 38 | ```{r simple, opts.label = "demo"} |
| 39 | ``` |
| 40 | |
| 41 | # Several plots |
| 42 | |
| 43 | ```{r multiple} |
| 44 | p1 <- plot_ly(economics, x = ~date, y = ~unemploy) %>% |
| 45 | add_lines(name = "unemploy") |
| 46 | p2 <- plot_ly(economics, x = ~date, y = ~uempmed) %>% |
| 47 | add_lines(name = "uempmed") |
| 48 | subplot(p1, p2) |
| 49 | ``` |
| 50 | |
| 51 | ## Code |
| 52 | |
| 53 | ```{r multiple, opts.label = "demo"} |
| 54 | ``` |
| 55 | |
| 56 | |
| 57 | # Events handler |
| 58 | |
| 59 | ```{r events, message=FALSE} |
| 60 | xfun::pkg_attach2("htmlwidgets") |
| 61 | plot_ly(mtcars, x = ~wt, y = ~mpg) %>% |
| 62 | onRender(" |
| 63 | function(el) { |
| 64 | el.on('plotly_hover', function(d) { |
| 65 | console.log('Hover: ', d); |
| 66 | }); |
| 67 | el.on('plotly_click', function(d) { |
| 68 | console.log('Click: ', d); |
| 69 | }); |
| 70 | el.on('plotly_selected', function(d) { |
| 71 | console.log('Select: ', d); |
| 72 | }); |
| 73 | } |
| 74 | ") |
| 75 | ``` |
| 76 | |
| 77 | ## Code |
| 78 | |
| 79 | ```{r events, opts.label = "demo"} |
| 80 | ``` |
| 81 | |
| 82 | # Symbols |
| 83 | |
| 84 | ```{r symbols} |
| 85 | vals <- schema(F)$traces$scatter$attributes$marker$symbol$values |
| 86 | vals <- grep("-", vals, value = T) |
| 87 | plot_ly() %>% |
| 88 | add_markers( |
| 89 | x = rep(1:12, each = 11, length.out = length(vals)), |
| 90 | y = rep(1:11, times = 12, length.out = length(vals)), |
| 91 | text = vals, |
| 92 | hoverinfo = "text", |
| 93 | marker = list( |
| 94 | symbol = vals, |
| 95 | size = 30, |
| 96 | line = list( |
| 97 | color = "black", |
| 98 | width = 2 |
| 99 | ) |
| 100 | ) |
| 101 | ) |
| 102 | ``` |
| 103 | |
| 104 | ## Code |
| 105 | |
| 106 | ```{r symbols, opts.label = "demo"} |
| 107 | ``` |
| 108 | |