-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.Rmd
963 lines (725 loc) Β· 17.4 KB
/
index.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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
---
title: "Outstanding User Interfaces with Shiny"
subtitle: '`r Sys.Date()`'
author: "David Granjon"
date: '`r Sys.Date()`'
output:
xaringan::moon_reader:
lib_dir: assets/libs
css:
- assets/css/js4shiny-xaringan-base.css
- assets/css/tachyons.min.css
#- assets/css/animate.css
- assets/css/js4shiny-xaringan-extra.css
- assets/css/custom.css
- https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css
includes:
after_body: assets/html/js4shiny-includes.html
seal: false
chakra: assets/js/remark-latest.min.js
nature:
beforeInit: ["https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"]
ratio: 16:9
slideNumberFormat: "%current%"
highlightStyle: docco
highlightLines: true
countIncrementalSlides: false
---
class: inverse, center, title-slide, middle
# Outstanding User Interfaces with Shiny
## David Granjon
### `r Sys.Date()`
### R in Pharma
```{r setup, include=FALSE}
library(emo)
library(shiny)
library(countdown)
library(sass)
options(htmltools.dir.version = FALSE)
knitr::opts_chunk$set(
fig.width = 10, fig.height = 6, fig.retina = 2,
warning = FALSE, message = FALSE
)
#xaringanExtra::use_search(show_icon = TRUE)
xaringanExtra::use_tile_view()
xaringanExtra::use_share_again()
xaringanExtra::use_clipboard()
xaringanExtra::use_tachyons()
xaringanExtra::use_fit_screen()
xaringanExtra::use_panelset()
xaringanExtra::use_extra_styles(
hover_code_line = TRUE, #<<
mute_unhighlighted_code = TRUE #<<
)
#source("assets/R/utils.R")
```
```{r js4shiny, echo=FALSE}
js4shiny::html_setup(stylize = c("fonts", "variables", "code"))
```
---
# Hi there `r ji("pizza")`
We're in for 20 minutes of **fun**!
- Grab a `r ji("coffee")`
- Make yourself comfortable `r ji("couch")` or π§
.flex.tc[
.w-30.mr3.center[
<img class="br-100" width="100px" height="100px" src="assets/images/granjda1.jpg" class="svg-img"/>
<br>
.f5[David]
.small[Senior Expert Data Scientist, Novartis]
]
]
---
class: inverse center title-slide middle
# Introduction
---
class: header_background
# Shiny
<br/>
.center[
<img src="assets/images/shiny.svg" width="20%" style="display: block; margin: auto;" class="svg-img"/>
]
- `{Shiny}` is about __10__ years old π²π²π².
- Today, many tools can make your apps __shining__.
- Let's review some of them ...
---
class: header_background
# What I will share today
<br>
.pull-left[
### How it started ...
<img src="assets/images/virtual-patient-1.png" width="100%"/>
]
--
.pull-right[
### ... 1 years after
<img src="assets/images/virtual-patient-3.png" width="100%"/>
]
---
class: header_background
# From the simplest app ... πΆ
<br>
.center[
```{r, eval=TRUE, echo=FALSE}
knitr::include_app("http://localhost:3511", height = "400px")
```
]
---
class: header_background
# Is this a Shiny app β
<br>
.center[
<iframe width="50%" src="http://localhost:3512" allowfullscreen="" frameborder="0" scrolling="yes" height="500px" style="margin-top: -50px"></iframe>
]
---
class: header_background
# What's the difference β
<br>
- __111__ lines of CSS.
--
- 29 lines of JavaScript code.
--
- 2 png images (dj gear + rotating wheel).
--
- 36 lines of R code (`{wavesurfer}` htmlWidget).
--
- Few custom HTML tags.
---
class: header_background
# What you'll learn today
<br>
.panelset[
.panel[.panel-name[π¨ Part 1]
__Tweak__ apps style with CSS and Sass
- CSS basics.
- How Sass may help you: `{sass}`.
- High level CSS and Sass tools: `{bslib}` and `{fresh}`.
]
.panel[.panel-name[π Part 2]
Unleash app __interactivity__ with JavaScript (JS)
- Discover how Shiny deals with inputs.
- Add React and any JS framework with `{packer}`.
- Optimize your app with custom handlers.
]
.panel[.panel-name[π§ββοΈ Part 3]
Create a new template from scratch
- Discover `{charpente}`.
]
.panel[.panel-name[π Part 4]
Sorry I don't have time ...
- Discover `{truelle}`.
- Use preexisting templates.
]
]
---
class: center middle
```{r, echo = FALSE, out.width='40%', fig.align='center'}
knitr::include_graphics("assets/images/excited-minions.gif")
```
---
class: inverse center title-slide middle
# π¨ Tweak with CSS and Sass π¨
---
class: header_background
# What is CSS?
<br>
CSS stands for Cascading Style Sheets,
.small.pull-left[
### Apply CSS
.pull-left[
- Mechanism:
```
selector {
property: value;
}
```
]
.pull-right[
- With Shiny:
```{r, eval=FALSE}
tags$style(
"p {
color: red;
}
"
)
```
]
]
.pull-right[
<iframe width="100%" src="http://localhost:3513" allowfullscreen="" frameborder="0" scrolling="yes" height="400px"></iframe>
]
---
class: header_background
# But CSS becomes messy π
<br>
- Easy to __repeat__ yourself.
- Difficult to read and maintain: π __spaghetti__ π.
- It's just a __mess__ ...
```{r, echo = FALSE, out.width='40%', fig.align='center'}
knitr::include_graphics("assets/images/alors_heureux.gif")
```
---
class: header_background
# Tidy your CSS with Sass π§Ή
<br>
- Sass = Syntactically Awesome Style Sheets.
- __Programmable__: variables, functions, loops, operators, modules, ...
- Modern web development workflow.
- __DRY__ programming.
- Will __motivate__ you to learn more CSS.
--
.pull-left[
An R interface exists: `{sass}`.
```{r, eval=FALSE}
install.packages("sass")
```
]
.pull-right[
<img src="assets/images/sass.svg" width="40%" style="display: block; margin: auto;" class="svg-img"/>
]
---
class: header_background
# {sass} in action
<br>
.panelset[
.panel[.panel-name[Variables]
.small.pull-left[
```{r, eval=FALSE}
library(sass)
rule1 <- ".class-1{ color: $color; }"
rule2 <- ".class-2{ background-color: $color; }"
sass(input = list(color = "purple", rule1, rule2))
```
]
.pull-right[
```
#> /* CSS */
#> .class-1 {
#> color: purple;
#> }
#>
#> .class-2 {
#> background-color: purple;
#> }
```
]
]
.panel[.panel-name[Functions]
.small.pull-left[
```{r, eval=FALSE}
sass(
list(
a = 2,
b = 4,
"@function multiply($parm1, $parm2) {
@debug 'parm1 is #{$parm1}';
@debug 'parm2 is #{$parm2}';
@return $parm1 * $parm2;
}",
".my-class {
width: multiply($a, $b) * 1px;
}"
)
)
```
]
.pull-right[
```
stdin:4 DEBUG: parm1 is 2
stdin:5 DEBUG: parm2 is 4
#> /* CSS */
#> .my-class {
#> width: 8px;
#> }
```
]
]
.panel[.panel-name[βΏLoops]
.pull-left[
```{r, eval=FALSE}
sass(input = list(
colors = c("green", "red"),
"@each $color in $colors {
.alert-#{$color} {
color: $color;
}
}"
))
```
]
.pull-right[
```
#> .alert-green {
#> color: green;
#> }
#>
#> .alert-red {
#> color: red;
#> }
```
]
]
.panel[.panel-name[With Shiny]
.xsmall.pull-left[
```{r, eval=FALSE}
# CSS to include in your app
css <- sass(
sass_layer(
defaults = list(
turquoise = "#03a4ff",
cyan = "#e705be",
green = "#f3d6e9",
yellow = "#fdaf2c",
red = "#ff483e",
"scheme-main" = "hsl(0, 0%, 10%)"
),
rules = sass_file(
input = system.file(
"sass/bulma/bulma.sass",
package = "OSUICode"
)
)
)
)
```
]
.pull-right[
<iframe width="100%" src="http://localhost:3514" allowfullscreen="" frameborder="0" scrolling="yes" height="300px"></iframe>
]
]
]
<br>
---
class: header_background
# Isn't there something easier?
<br>
--
.pull-left[
## {fresh}
- Built on top of `{sass}`.
- For `{shiny}`, `{shinydashboard}`, ...
```{r, eval=FALSE}
install.packages("fresh")
```
]
--
.pull-right[
## {bslib}
- Built on top of `{sass}`.
- For `{shiny}`.
- Live theming widget.
- Dynamic theming.
- Conditional rendering.
- ...
```{r, eval=FALSE}
install.packages("bslib")
```
]
---
class: header_background
# A dark theme with {fresh}
<br>
.xsmall.pull-left[
```{r, eval=FALSE}
library(fresh)
dark_theme <- create_theme(
bs4dash_vars(
navbar_light_color = "#bec5cb",
navbar_light_active_color = "#FFF",
navbar_light_hover_color = "#FFF"
),
bs4dash_yiq(
contrasted_threshold = 10,
text_dark = "#FFF",
text_light = "#272c30"
),
bs4dash_layout(main_bg = "#353c42"),
bs4dash_sidebar_dark(
bg = "#272c30",
color = "#bec5cb",
hover_color = "#FFF",
submenu_bg = "#272c30",
submenu_color = "#FFF",
submenu_hover_color = "#FFF"
),
bs4dash_status(dark = "#272c30"),
bs4dash_color(gray_900 = "#FFF", white = "#272c30")
)
```
]
.pull-right[
```{r, echo = FALSE, out.width='100%', fig.align='center'}
knitr::include_graphics("assets/images/bs4Dash-fresh.png")
```
]
---
class: header_background
# A neon theme with {bslib}
<br>
.xsmall.pull-left[
```{r, eval=FALSE}
library(bslib)
bslib_neon_theme <- bs_theme(
version = 4,
bg = "#000000",
fg = "#FFFFFF",
primary = "#9600FF",
secondary = "#1900A0",
success = "#38FF12",
info = "#00F5FB",
warning = "#FFF100",
danger = "#FF00E3",
base_font = "Marker Felt",
heading_font = "Marker Felt",
code_font = "Chalkduster"
)
bs_theme_preview(bslib_neon_theme, with_themer = FALSE)
```
]
.pull-right[
<iframe width="100%" src="http://localhost:3515" allowfullscreen="" frameborder="0" scrolling="yes" height="400px"></iframe>
]
---
class: header_background
# Dynamic theming example
<br>
Static plot (base R, ggplot2, ...) theming requires `{thematic}`.
.center[
<iframe width="100%" src="http://localhost:3516" allowfullscreen="" frameborder="0" scrolling="yes" height="400px"></iframe>
]
---
class: header_background
# Dynamic theming with {bs4Dash}
<br>
.center[
<iframe width="100%" src="https://dgranjon.shinyapps.io/bs4DashDemo/" allowfullscreen="" frameborder="0" scrolling="yes" height="450px" style="font-size: 0.5em"></iframe>
]
---
class: header_background
# Conditional rendering
<br>
Only 1 code base, 2 possible outputs ποΈ
.pull-left[
## BS4 accordion
<iframe width="100%" src="http://localhost:3517" allowfullscreen="" frameborder="0" scrolling="yes" height="400px"></iframe>
]
.pull-right[
## BS5 accordion
<iframe width="100%" src="http://localhost:3518" allowfullscreen="" frameborder="0" scrolling="yes" height="400px"></iframe>
]
---
class: inverse center title-slide middle
# π Unleash interactivity with JS π
---
class: header_background
# CSS does not solve all issues
<br>
What can JS do for me β
.pull-left[
- Add new __input widgets__.
- __Lighten__ the R logic be delegating some tasks to JS.
- More __interactive__ apps.
- Overall, a better user experience.
]
.pull-right[
<iframe width="100%" src="http://localhost:3523" allowfullscreen="" frameborder="0" scrolling="yes" height="400px"></iframe>
]
---
class: header_background
# Unleash app interactivity: example
<br>
.center[
<video height="400px" width="100%" controls>
<source src="assets/movies/js-interactivity-demo.mov" type="video/mp4">
]
---
class: header_background
# JS code can also become messy
<br>
--
- How do I include __JS__ in my Shiny project β
--
- What are the __best practices__ around JS code management β
--
- Let's try to bring some answers ...
---
class: header_background
# Relevant R/JS projects
<br>
.panelset[
.panel[.panel-name[π£ Meet `{packer}`]
.small.pull-left[
- A `{usethis}` for JavaScript/R projects.
- Supports Shiny outputs, inputs, htmlwidgets, extensions, ...
- Predefined templates: React, Vue, Framework7.
```{r, eval=FALSE}
install.packages("packer")
remotes::install_github("JohnCoene/packer")
```
]
.pull-right[
<a href="https://github.com/JohnCoene/packer"><img src="assets/images/packer.png" width="40%" class="svg-img"/></a>
]
]
.panel[.panel-name[π₯ Scaffold project]
.small.pull-left[
We call:
```r
golem::create_golem(...)
packer::scaffold_golem(framework7 = TRUE)
packer::bundle()
```
- Sets all the necessary structure (npm, loaders, webpack, ...).
- So that you don't have to worry too much.
]
.pull-right[
<video autosize="true" controls>
<source src="assets/movies/packer-demo.mov" type="video/mp4">
</video>
]
]
]
---
class: header_background
# Application to Framework7
<br>
.small.pull-left[
- Framework7: first class mobile web framework.
- R in Pharma 2021 RinteRface [workshop](https://rinpharma2021.rinterface.com/).
- Advantages:
- No need to develop a Shiny extension.
- Faster app.
- Ease collaboration with front end dev.
]
.pull-right[
<div class="md-iphone-5 md-black-device" style="font-size: 0.5em; margin-top: -100px; !important;">
<div class="md-body">
<div class="md-buttons"></div>
<div class="md-front-camera"></div>
<div class="md-top-speaker"></div>
<div class="md-screen">
<iframe width="100%" src="http://localhost:3519" allowfullscreen="" frameborder="0" scrolling="yes" height="470px"></iframe>
</div>
<button class="md-home-button"></button>
</div>
</div>
]
---
class: header_background
# Optimize: improve user experience
<br>
.panelset[
.panel[.panel-name[`renderUI` causes lags]
.pull-left[
<iframe width="100%" src="http://localhost:3520" allowfullscreen="" frameborder="0" scrolling="yes" height="300px"></iframe>
]
.small.pull-right[
```{r, eval=FALSE}
dummy_task <- reactive({
Sys.sleep(5)
12
})
output$custom_box <- renderUI({
dummy_task() #<<
box(
title = "Box",
width = dummy_task(),
"Box body",
background = input$background
)
})
```
]
]
.panel[.panel-name[Prefer `update_*` functions]
.pull-left[
<iframe width="100%" src="http://localhost:3521" allowfullscreen="" frameborder="0" scrolling="yes" height="300px"></iframe>
]
.xsmall.pull-right[
```{r, eval=FALSE}
dummy_task <- reactive({
Sys.sleep(5)
12
})
observeEvent(dummy_task(), {
updateBox2( #<<
"mybox",
action = "update",
options = list(
width = dummy_task(),
title = tagList(
shinydashboardPlus::dashboardBadge(
"New",
color = "red"
),
"New title"
)
)
)
})
```
]
]
]
---
class: inverse center title-slide middle
# π§ββ New template from scratch π§ββ
---
class: header_background
# Introducing {charpente}
<br>
.panelset[
.panel[.panel-name[π£ Meet `{charpente}`]
.xsmall.pull-left[
- Plug and play __package__ structure.
- Import dependencies from [jsdelivr](https://www.jsdelivr.com/).
- __Easy__ HTML to R conversion.
- R and JS __boilerplates__: `{shiny}` input bindings, `{shiny}` message handlers, ...
- Seamless JavaScript code management with [esbuild](https://esbuild.github.io/).
```{r, eval=FALSE}
remotes::install_github("RinteRface/charpente")
```
]
.pull-right[
<a href="https://github.com/RinteRface/charpente"><img src="assets/images/charpente.png" width="40%" style="display: block; margin: auto;" class="svg-img"/></a>
]
]
.panel[.panel-name[π¨βπ Workflow example]
.xsmall.pull-left[
```{r, eval=FALSE}
library(charpente)
path <- file.path(tempdir(), "mypkg")
create_charpente(path, license = "mit")
# Once the package is created and opened
# Look for all bulma flavors
get_dependency_versions("bulma")
# Download bulma locally
create_dependency("bulma")
# Create JS handler
create_custom_handler("modal")
# Create input binding
create_input_binding("myinput")
# Compress JS for production
build_js()
devtools::load_all()
```
]
.pull-right[
<video height="400px" width="100%" controls>
<source src="assets/movies/charpente-demo.mov" type="video/mp4">
</video>
]
]
.panel[.panel-name[Applications]
.center[
<a href="https://github.com/RinteRface/bs4Dash"><img src="assets/images/bs4Dash.svg" width="20%" style="margin: auto;" class="svg-img"/></a>
<a href="https://github.com/RinteRface/shinyMobile"><img src="assets/images/shinyMobile.svg" width="20%" style="margin: auto;" class="svg-img"/></a>
<a href="https://github.com/RinteRface/shinydashboardPlus"><img src="assets/images/shinydashboardPlus.svg" width="20%" style="margin: auto;" class="svg-img"/></a>
]
]
]
---
class: end_part_1
---
class: inverse center title-slide middle
# π Sorry I don't have time π
---
class: header_background
# Use existing templates: <a href="https://github.com/RinteRface/shinyMobile"><img src="assets/images/shinyMobile.svg" width="5%" style="display: inline; margin: auto;" class="svg-img"/></a>
<br>
Built on top of [Framework7](https://framework7.io/).
.small.pull-left[
- Native look and feel for iOS and Android
- PWA support:
- Can be __installed__ on the device ...
- ... But run via a web browser.
- Provide __offline__ features (don't expect too much).
- One code base (web languages).
- ... also works for desktop apps π
]
.pull-right[
<div class="md-iphone-5 md-white-device" style="font-size: 0.5em; margin-top: -150px; !important;">
<div class="md-body">
<div class="md-buttons"></div>
<div class="md-front-camera"></div>
<div class="md-top-speaker"></div>
<div class="md-screen">
<iframe width="100%" src="http://localhost:3522" allowfullscreen="" frameborder="0" scrolling="yes" height="470px"></iframe>
</div>
<button class="md-home-button"></button>
</div>
</div>
]
---
class: header_background
# I want to go faster!
<br>
### {truelle} is a {golem} GUI generator <a href="https://github.com/RinteRface/truelle"><img src="assets/images/truelle.png" width="5%" style="margin: auto;" class="svg-img"/></a>
--
.small.pull-left[
```{r, eval=FALSE}
remotes::install_github("RinteRface/truelle")
library(truelle)
run_app()
```
]
.pull-right[
<video height="400px" width="100%" controls>
<source src="assets/movies/truelle-demo.mov" type="video/mp4">
</video>
]
---
class: center middle
# Want to learn more β
.center[
π [Read my book](https://unleash-shiny.rinterface.com) π
]
```{r, echo = FALSE, out.width='20%', fig.align='center'}
knitr::include_graphics("assets/images/shiny-ui-book.png")
```
.center[
Thanks π
]
---
class: end_part_2