generated from inSilecoInc/workshop_R_template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
_05_shiny_in_prod.Rmd
167 lines (109 loc) · 3.66 KB
/
_05_shiny_in_prod.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
---
title: "Shiny in prod"
output:
xaringan::moon_reader:
css: [default, rd.css, rd-font.css, "hygge"]
lib_dir: assets
seal: false
nature:
highlightStyle: dracula
countIncrementalSlides: false
beforeInit: "macros.js"
---
```{r include = FALSE}
source("_setup.R")
```
class: inverse, center, middle
# `r rfa("rocket")` Shiny in production
![:custom_hr]()
## An overview
---
# Deploying a Shiny app
## Making your application available
<br>
--
* Posit (formerly RStudio) solutions:
* `r lk()` https://posit.co/products/open-source/shinyserver/
* `r lc()` https://www.shinyapps.io/
* `r lk()` https://docs.posit.co/shinyapps.io/index.html
--
* Self-hosting:
- `r rfa("blog")` https://emeraldreverie.org/2019/11/17/self-hosting-shiny-notes-from-edinbr/
- `r lk()` https://www.shinyproxy.io/#open-source
???
Account via github
---
# Best practices
### `r bk()` https://mastering-shiny.org/scaling-intro.html
--
## Follow general best practices
### (software engineering principles)
* Good design of your code (easy to read, non-redundant, etc.)
* version control such (e.g. `r gt()`)
* use a good code editor
* test your application
* etc.
---
# Best practices
## Pay attention to your `r rp()` code
* Use functions to avoid redundancy
* Use mapping functions [`purrr`](https://CRAN.R-project.org/package=purrr)
--
## Profile and optimize your `r rp()` code
* profile your code
* `Rprof()`
* `r rfa("box")` [`profvis`](https://CRAN.R-project.org/package=profvis) ([flame graph](https://queue.acm.org/detail.cfm?id=2927301))
* benchmark
* `r rfa("box")` [`microbenchmark`](https://CRAN.R-project.org/package=microbenchmark)
* read about this
* `r rfa("bookmark")` [Improving performance](https://adv-r.hadley.nz/perf-improve.html) (`r bk()` Advanced R)
* `r bk()` [Efficient R programming](https://csgillespie.github.io/efficientR/)
---
# Best practices
### **More complex shiny longer codebase**
--
### Try to create **independent components**
* functions in separate `.R` files in `R/`
* external package(s)
* modules
---
# Best practices
### Use [modules](https://mastering-shiny.org/scaling-modules.html)
```R
yourModuleUI <- function(id) {
ns <- NS(id)
tagList(...)
}
yourModuleServer <- function(id, geoms, preview, u_details) {
moduleServer(
id,
function(input, output, session) {
...
}
)
}
```
---
# Best practices
## Turn your shiny app into an `r rp()` package
### `r ar()` Access to robust tools to develop your app (namely `r rfa("box")` [`devtools`](https://CRAN.R-project.org/package=devtools))
--
### **Testing**
* Critical part of the development of your app
* Unit tests for your functions (see `r rfa("bookmark")` https://r-pkgs.org/testing-basics.html#introducing-testthat)
* Test reactivity with `testServer()`(see `r rfa("bookmark")` https://mastering-shiny.org/scaling-testing.html)
* Test your application visual (using snapshot, see https://testthat.r-lib.org/articles/snapshotting.html)
---
# Best practices
### Avoid heavy computations when possible
--
### Load tests
* Test how your application with a given load (i.e. a given number of users using your application simultaneously)
* `r rfa("box")` [`shinyloadtest`](https://CRAN.R-project.org/package=shinyloadtest)
--
### Caching
* Save the output result of a given action and reuse it instead of redoing the computation
* Use `bindCache()` (see also `r rfa("box")` [`cachem`](https://CRAN.R-project.org/package=cachem))
* `r lk()` https://shiny.rstudio.com/articles/caching.html
--
* `r rfa("youtube")` https://www.rstudio.com/resources/rstudioconf-2019/shiny-in-production-principles-practices-and-tools/