-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy path06-layout_themes_html.Rmd
475 lines (355 loc) · 13.4 KB
/
06-layout_themes_html.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# Layout, themes, HTML
```{r}
library(bslib)
library(htmltools)
library(shiny)
```
## Disclaimer
"Mastering Shiny" is a moving target.
Much of the content for "Layout, themes, HTML" chapter was originally part of
the "Basic UI" chapter.
<!-- So I'm attempting to put a web-dev oriented spin on the material -->
Hence, these notes have greater emphasis on low-level web concerns (html / css /
bootstrap)
## Learning objectives
- Creating raw html using R
- HTML elements: attributes, classes, content
- CSS for styling
- Bootstrap (and related) front end toolkits
- Shiny produces single-page applications
- ... but multipage _layouts_ are possible
## Resources
### Websites
- [Awesome Shiny Extensions](https://github.com/nanxstats/awesome-shiny-extensions)
- [Shiny Application Layout Guide](https://shiny.rstudio.com/articles/layout-guide.html)
- Mozilla Developer Network
- [Intro to HTML](https://developer.mozilla.org/en-US/docs/Learn/HTML/Introduction_to_HTML)
- [CSS First Steps](https://developer.mozilla.org/en-US/docs/Learn/CSS/First_steps)
- [Website parsing figure](https://developer.mozilla.org/en-US/docs/Learn/CSS/First_steps/How_CSS_works/rendering.svg)
### Books
- [Outstanding User Interfaces ...](https://unleash-shiny.rinterface.com)
- Especially Chapters 1, 5, 6, 7
### Packages / Tools
- [Bootstrap](https://getbootstrap.com/)
- [Sass](https://sass-lang.com/)
- R packages
- `{htmltools}` (for building raw html)
- `{sass}` (for building css using Sass rules; not mentioned in Wickham)
- `{bslib}` (handles working with bootstrap)
- `{thematic}` (matches plotting theme to app theme)
- `{shiny.semantic}` (formantic)
- `{shinyMobile}` (framework 7)
- `{shinymaterial}` (Material design)
- `{shinydashboard}` (for building dashboards)
## Under the hood
A typical web app:
- In the browser (front-end):
- .html file defines the content
- .css files define the style
- javascript handles interactivity
- On the server (back-end):
- Requests received from the client
- R performs computations based on those requests
- Sends responses to the client
### HTML - Hypertext markup language
To view the .html for an app
- Open an app in the browser or Rstudio
- "View source" / "Inspect" / Open "Developer Tools"
Typical .html structure:
```html
<!DOCTYPE html>
<html>
<head>
<!-- Metadata goes here (encodings, dependencies, author ...) -->
<meta charset="utf-8">
<title>The page title</title>
</head>
<body>
<!-- Content goes here!-->
<h1>My Printed Title</h1>
<p>
A paragraph of text.
</p>
</body>
</html>
```
### Writing HTML with `shiny` / `htmltools`
How to write .html using just R code?
The simplest shiny UI:
```{r}
ui <- shiny::fluidPage()
```
The .html body for that UI can be viewed directly:
```{r, comment = ""}
cat(
as.character(ui)
)
```
To also include the 'head' for the .html:
```{r, comment=""}
cat(
shiny:::renderPage(ui)
)
```
`script` and `link` elements in the `<head>` = dependencies for the site
Adding bare HTML to the shiny-produced HTML
```{r, comment=""}
ui_with_raw_html <- shiny::fluidPage(
# some raw html
htmltools::HTML(r"(
<h1>This is a heading</h1>
<ul>
<li>First bullet</li>
<li>Second bullet</li>
</ul>
)"
),
# a shiny text imput
textInput("name", "What's your name?")
)
cat(
as.character(ui_with_raw_html)
)
```
Using HTML helpers:
```{r}
ui_from_html_helpers <- shiny::fluidPage(
h1("This is a heading", class = "my-class"),
tags$ul(
tags$li("First bullet"),
tags$li("Second bullet")
),
textInput("name", "What's your name?")
)
cat(
as.character(ui_from_html_helpers)
)
```
### CSS - Cascading Style Sheets
CSS
- isn't covered in the chapter (but links provided)
- but underlies how bootstrap styles your shiny app
Much more detail at
- [MDN](https://developer.mozilla.org/en-US/docs/Learn/CSS);
- and in [Outstanding User Interfaces with Shiny](https://unleash-shiny.rinterface.com/beautify-css.html)
How does the browser display a site / app?
[MDN Website parsing figure](https://developer.mozilla.org/en-US/docs/Learn/CSS/First_steps/How_CSS_works/rendering.svg)
### Using CSS to style a shiny app
A simple CSS file to set any text with "my-class" as class to red
```css
/* ./examples/06-layout_themes_html-css_example/www/style.css */
.my-class {
color: red
}
```
- Put static files (javascript / css) in the `./www` subdirectory of your app
- Include your files using `tags$link` when defining the UI
- Note: the path in the link should be "style.css" not "www/style.css"
```r
# ./examples/06-layout_themes_html-css_example/app.R
library(shiny)
ui <- shiny::fluidPage(
tags$link(rel = "stylesheet", type = "text/css", href = "style.css"),
h1("This is a heading", class = "my-class"),
textInput("name", "What's your name?")
)
server <- function(input, output, session) {}
shinyApp(ui, server)
```
You can write custom rules to style based on
- html element (`p`, `h1`, ...),
- attributes
- IDs
- and on interactive things (eg, when the mouse hovers)
## Bootstrap & Themes
Shiny uses the [bootstrap](https://getbootstrap.com/) toolkit for styling.
Here's the strapline for bootstrap:
Quickly design and customize responsive mobile-first sites with Bootstrap,
the world’s most popular front-end open source toolkit, featuring Sass
variables and mixins, responsive grid system, extensive prebuilt components,
and powerful JavaScript plugins.
What does that mean?
- Responsive
- The page alters layout / appearance for different screen widths
- Sass variables and mixins
- These are tools that reduce the amount of repetition when writing CSS
- Prebuilt components
- You don't have to write your own button, navbar, etc ... classes from
scratch
- Javascript plugins
- eg, for using dropdowns
So bootstrap solves lots of challenges for us
Use package `{bslib}` to work with bootstrap in R
To modify themes:
```r
bslib::bs_theme(
# colours in RGB
bg = "#202123", # background
fg = "#B8BCC2", # foreground
primary = NULL,
secondary = NULL,
success = NULL,
info = NULL,
warning = NULL,
danger = NULL,
# fonts
base_font = list(bslib::font_google("Pacifico", local = TRUE), "sans-serif"),
code_font = NULL,
heading_font = NULL
# further options ...
)
```
Or use `bootswatch` to pick a prebuilt theme:
```r
bslib::bs_theme(bootswatch = "sandstone")
```
Then use a theme in your UI definition:
```{r, comment = ""}
theme_dark <- bslib::bs_theme(
bootswatch = "darkly",
base_font = list(bslib::font_google("Pacifico", local = TRUE), "sans-serif")
)
ui_with_bs <- fluidPage(
theme = theme_dark
)
cat(
shiny:::renderPage(ui_with_bs)
)
```
## Layouts
The Layouts section was moved from the Basic UI chapter with little change.
Rather than go over them again, we'll rewrite a simple (single-page layout) app
into something more complicated (and style it while we're at it)
See `./examples/06-layout_themes_html-single_to_multipage_app/`
### Single-page Layouts
Page functions: `fluidPage`, `fixedPage`, `fillPage`
Layout functions: `sidebarLayout`, `fluidRow`, `column`
### Multi-page Layouts
Page functions: `navbarPage` (or nest the following in `fluidPage`)
Layout functions: `tabsetPanel`, `tabPanel`, `navlistPanel`, `navbarMenu`
## Case study
This is one of the first shiny apps I wrote, it pulls data for a specific user
from Stack Overflow, works out which "tags" they have received the most votes
for, then plots those top tags in a word cloud.
### The initial app (app-01.R)
How might this app be improved?
- Code
- `stackr` eagerly obtains data from stack overflow (maybe it should wait til
user has input a complete user ID)
- UX
- Visual style is boring (maybe it should look more like stack overflow?)
- Could we input user name, rather than ID?
- Could we show the user-name / image, so the user knows they've copied in the
correct user ID
- Can we add some extra plots / tables and make a multipage layout
```{r, code=readLines("examples/06-layout_themes_html-single_to_multipage_app/app-01.R"), eval = FALSE}
```
### The final app (app.R)
Relative to the original app, we have:
- introduced a new results table (containing the data obtained from
stack-overflow)
- converted from a single-panel to a multi-panel layout (using `tabsetPanel`
and two `tabPanel`s)
- attempted to use the stack-overflow colour scheme as part of the apps theme,
and also in the wordcloud colour scheme
The stack overflow colour scheme was obtained by using the browser's developer
tools (here using "Inspect" in chromium and looking at the "styles" section) on
an open SO page.
```{r, code=readLines("examples/06-layout_themes_html-single_to_multipage_app/app-01.R"), eval = FALSE}
```
## Meeting Videos
### Cohort 1
`r knitr::include_url("https://www.youtube.com/embed/hriFSGeBa_Q")`
<details>
<summary> Meeting chat log </summary>
```
00:05:16 Maya Longhi: Hello
00:12:32 Russ Hyde: Hi
00:13:03 Russ Hyde: I'm just checking my screen share looks OK. Could you tell me if you can read the text on my rstudio instance
00:14:30 Russ Hyde: Is the text too pale?
00:14:46 Morgan Grovenburg: No, I can read it
00:14:53 Russ Hyde: Thanks
00:16:45 Morgan Grovenburg: I couldn't figure out how to use `bslib::bs_theme_preview()`, will you be showing that today?
00:17:50 Russ Hyde: I can do if you like
00:19:48 Morgan Grovenburg: Yes, please! Really, I just need see code that runs it
00:30:02 Layla Bouzoubaa: @sham, There’s a JavaScript for R book!
00:30:21 shamsuddeen: Oh, yah Layla
00:30:24 shamsuddeen: Thanks
00:30:31 Layla Bouzoubaa: https://book.javascript-for-r.com/
00:30:32 Morgan Grovenburg: https://book.javascript-for-r.com/
00:38:15 Federica G: line has gone ?!
00:38:26 Diamantis: lost the sound but still can see the shared screen
01:05:55 Layla Bouzoubaa: Sham, Bootstrap is a framework that combines html/js/css that can customized
01:06:06 shamsuddeen: ok
01:06:11 shamsuddeen: Thanks Layla
01:07:00 Layla Bouzoubaa: Like the buttons and stuff in shiny are following bootstrap guidelines (ie the shape, look etc) but you can customize them
01:07:12 shamsuddeen: ok
01:08:39 Layla Bouzoubaa: I just discovered this book btw: https://engineering-shiny.org/ . Ch.18.3.3 talks about external css files that may clarify your question on adding “www/“, sham
01:17:51 shamsuddeen: Oh, Thanks Layla. I got it
01:21:47 Federica G: Thanks !
01:22:27 shamsuddeen: Thanks
01:22:38 Morgan Grovenburg: Thank you Russ!
01:24:22 Anne Hoffrichter: Thanks Russ!
```
</details>
### Cohort 2
#### Meeting #1 video: `r knitr::include_url("https://www.youtube.com/embed/FN34gu0FzQo")` {-}
<details>
<summary> Meeting #1 chat log </summary>
```
00:07:02 shane: one sec, dog wants to go see his mom ha
00:07:32 shane: Kevin, your volume is a little low like it was last week
01:06:06 shane: team, I unfortunately have to drop. Thank you everyone for the discussion, and thanks to Ryan for presenting and for the great info! If we do end up bleeding into a second session on this, I would definitely be down to be there. Have a great evening!
```
</details>
#### Meeting #2 video: `r knitr::include_url("https://www.youtube.com/embed/W01MoW3AWpY")` {-}
<details>
<summary> Meeting #2 chat log </summary>
```
00:21:58 shane: https://rstudio.github.io/thematic/
00:25:03 Collin Berke: https://bootswatch.com/
00:28:50 Conor Tompkins: "This works because, at plot time, thematic grabs CSS styles from the plot(s) HTML container (via shiny::getCurrentOutputInfo())1 and uses that info to set new R styling defaults."
00:28:56 Conor Tompkins: From https://rstudio.github.io/thematic/articles/auto.html#shiny
00:31:15 Collin Berke: Not sure how this relates, but here is the link to the `theme_set()` function I referenced: https://ggplot2.tidyverse.org/reference/theme_get.html
00:46:41 Collin Berke: You can also see the object here: https://gallery.shinyapps.io/095-plot-interaction-advanced/
00:50:48 Collin Berke: Here's some more about subsetting list objects. Check out this chapter from Advanced R: https://adv-r.hadley.nz/subsetting.html
01:13:14 Ryan Metcalf: https://www.osti.gov/servlets/purl/1115145
```
</details>
### Cohort 3
`r knitr::include_url("https://www.youtube.com/embed/gpj2Uw8imtA")`
<details>
<summary>Meeting chat log</summary>
```
00:17:24 Njoki Njuki Lucy: Current deployed slide deck: https://r4ds.github.io/bookclub-mshiny/layout-themes-html.html
00:36:58 Njoki Njuki Lucy: https://revealjs.com/
01:04:33 Njoki Njuki Lucy: what is the function cat() doing?
01:10:53 Federica Gazzelloni: cat {base}: Concatenate and Print
01:11:12 Njoki Njuki Lucy: Great, thanks! :)
01:11:34 Federica Gazzelloni: It outputs the objects, concatenating the representations.
01:13:06 Brendan Lam: I'm also good either way
```
</details>
`r knitr::include_url("https://www.youtube.com/embed/EDcW3IwtBQc")`
### Cohort 4
`r knitr::include_url("https://www.youtube.com/embed/LAm7k2qGvbo")`
<details>
<summary>Meeting chat log</summary>
```
00:11:49 Lydia Gibson: I'm having issues with zoom. I'll have to join from my laptop
00:13:15 LUCIO ENRIQUE CORNEJO RAMÍREZ: do i start now?
00:42:25 Trevin Flickinger: Here for more about the customizer: https://rstudio.github.io/bslib/articles/bslib.html#real-time
00:42:42 Lydia Gibson: Thank you!
01:03:16 Trevin Flickinger: Yes, thank you!
01:03:22 LUCIO ENRIQUE CORNEJO RAMÍREZ: thank you
01:03:58 LUCIO ENRIQUE CORNEJO RAMÍREZ: bye, thanks
```
</details>
### Cohort 5
`r knitr::include_url("https://www.youtube.com/embed/URL")`
<details>
<summary>Meeting chat log</summary>
```
LOG
```
</details>