-
Notifications
You must be signed in to change notification settings - Fork 13
/
README.Rmd
642 lines (539 loc) · 16.4 KB
/
README.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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%",
eval = FALSE
)
```
# brochure
<!-- badges: start -->
[![R build status](https://github.com/ColinFay/brochure/workflows/R-CMD-check/badge.svg)](https://github.com/ColinFay/brochure/actions)
[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://www.tidyverse.org/lifecycle/#experimental)
[![R-CMD-check](https://github.com/ColinFay/brochure/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/ColinFay/brochure/actions/workflows/R-CMD-check.yaml)
<!-- badges: end -->
__THIS IS A WORK IN PROGRESS, DO NOT USE__
The goal of `{brochure}` is to provide a mechanism for creating natively multi-page `{shiny}` applications, _i.e_ that can serve content on multiple endpoints.
__Disclaimer__: the way you will build app with `{brochure}` is different from the way you usually build `{shiny}` apps, as we no longer operate under the single page app paradigm.
Please read the "Design Pattern" of this README for more info.
## Installation
You can install the dev version of `{brochure}` with:
``` r
remotes::install_github("ColinFay/brochure")
```
```{r eval = TRUE}
library(brochure)
library(shiny)
```
## About
You're reading the doc about version : `r pkgload::pkg_version()`
This README has been compiled on the
```{r eval = TRUE}
Sys.time()
```
Here are the test & coverage results :
```{r eval = TRUE}
devtools::check(quiet = TRUE)
```
```{r echo = FALSE,eval = TRUE}
unloadNamespace("shinipsum")
```
```{r eval = TRUE}
covr::package_coverage()
```
## Minimal `{brochure}` App
### `page()`
A `brochureApp` is a series of `page`s that are defined by an `href` (the path/endpoint where the page is available), a `{shiny}` UI and a `server` function.
This is conceptually important: each page has its own shiny session, its own UI, and its own server.
Note that the server is optional if you want to display a static page.
```{r}
brochureApp(
# First page
page(
href = "/",
ui = fluidPage(
h1("This is my first page"),
plotOutput("plot")
),
server = function(input, output, session) {
output$plot <- renderPlot({
plot(iris)
})
}
),
# Second page, without any server-side function
page(
href = "/page2",
ui = fluidPage(
h1("This is my second page"),
tags$p("There is no server function in this one")
)
)
)
```
> You can now navigate to /, and to /page2 inside your browser.
### `redirect()`
Redirections can be used to redirect from one endpoint to the other:
```{r}
brochureApp(
page(
href = "/",
ui = tagList(
h1("This is my first page")
)
),
redirect(
from = "/nothere",
to = "/"
)
)
```
> You can now navigate to /nothere, you'll be redirected to /
A more elaborate example:
```{r}
# Creating a navlink
nav_links <- tags$ul(
tags$li(
tags$a(href = "/", "home"),
),
tags$li(
tags$a(href = "/page2", "page2"),
),
tags$li(
tags$a(href = "/contact", "contact"),
)
)
page_1 <- function() {
page(
href = "/",
ui = function(request) {
tagList(
h1("This is my first page"),
nav_links,
plotOutput("plot")
)
},
server = function(input, output, session) {
output$plot <- renderPlot({
plot(mtcars)
})
}
)
}
page_2 <- function() {
page(
href = "/page2",
ui = function(request) {
tagList(
h1("This is my second page"),
nav_links,
plotOutput("plot")
)
},
server = function(input, output, session) {
output$plot <- renderPlot({
plot(mtcars)
})
}
)
}
page_contact <- function() {
page(
href = "/contact",
ui = tagList(
h1("Contact us"),
nav_links,
tags$ul(
tags$li("Here"),
tags$li("There")
)
)
)
}
brochureApp(
# Pages
page_1(),
page_2(),
page_contact(),
# Redirections
redirect(
from = "/page3",
to = "/page2"
),
redirect(
from = "/page4",
to = "/"
)
)
```
__IMPORTANT NOTE__ all elements which are not of class `"brochure_*"` (`brochure_page` and `brochure_redirect`) will be injected __as is__ in the page.
In other word, if you use a function that return a string, the string will be added as is to the pages.
For example, this will inject a `"x"` on each page.
This is probably __NOT__ what you want to do, but can be the source of some bugs you'll have with your app.
```{r}
brochureApp(
"x",
page(
href = "/"
)
)
```
## `req_handlers` & `res_handlers`
### Sorry what?
Each page, and the global app, have a `req_handlers` and `res_handlers` parameters, that can take a __list of functions__.
An `*_handler` is a function that takes as parameter(s):
- For `req_handlers`, `req`, which is the request object (see below for when these objects are created).
For example `function(req){ print(req$PATH_INFO); return(req)}`.
- For `res_handlers`, `res`, the response object, & `req`.
For example `function(res, req){ print(res$content); return(res)}`.
`req_handlers` __must__ return `req` & `res_handlers` __must__ return `res`.
Both can be potentially modified.
They can be used to register log, or to modify the objects, or any kind of things you can think of.
If you are familiar with `express.js`, you can think of `req_handlers` as what express calls "middleware".
These functions are run when R is building the HTTP response to send to the browser (i.e, no server code has been run yet), following this process:
1. R receives a `GET` request from the browser, creating a request object, called `req`
1. The `req_handlers` are run using this `req` object
1. R creates an `httpResponse`, using this `req` and how you defined the UI
1. The `res_handlers` are run on this `httpResponse` (first app level `res_handlers`, then page level `res_handlers`)
1. The `httpResponse` is sent back to the browser
Note that if any `req_handlers` returns an `httpResponse` object, it will be returned to the browser immediately, without any further computation.
This early `httpResponse` will not be passed to the `res_handlers` of the app or the page.
This process can for example be used to send custom `httpResponse`, as shown below with the `healthcheck` endpoint.
You can use formulas inside your handlers. `.x` and `..1` will be `req` for req_handlers, `.x` and `..1` will be `res` & `.y` and `..2` will be `req` for res_handlers.
Design pattern side-note: you'd probably want to define the handlers outside of the app, for better code organization (as with `log_where` below).
### Example: Logging with `req_handlers()`, and building a healthcheck point
In this app, we'll log to the console every page and the time it is called, using the `log_where()` function.
```{r}
log_where <- function(req) {
cli::cat_rule(
sprintf(
"%s - %s",
Sys.time(),
req$PATH_INFO
)
)
req
}
```
We'll also build an `healthcheck` endpoint that simply returns a `httpResponse` with the 200 HTTP code.
```{r}
# Reusing the pages from before
brochureApp(
req_handlers = list(
log_where
),
# Pages
page_1(),
page_2(),
page_contact(),
page(
href = "/healthcheck",
# As this is a pure backend exchange,
# We don't need a UI
ui = tagList(),
# As this req_handler returns an httpResponse,
# This response will be returned directly to the browser,
# without passing through the usual shiny http dance
req_handlers = list(
# If you have shiny < 1.6.0, you'll need to
# do shiny:::httpResponse (triple `:`)
# as it is not exported until 1.6.0.
# Otherwise, see ?shiny::httpResponse
~ shiny::httpResponse(200, content = "OK")
)
)
)
```
If you navigate to each page, you'll see this in the console:
```
Listening on http://127.0.0.1:4879
── 2021-02-17 21:52:16 - / ──────────────────────────────
── 2021-02-17 21:52:17 - /page2 ─────────────────────────
── 2021-02-17 21:52:19 - /contact ───────────────────────
```
If you go to another R session, you can check that you've got a 200 on `healthcheck`
``` r
> httr::GET("http://127.0.0.1:4879/healthcheck")
Response [http://127.0.0.1:4879/healthcheck]
Date: 2021-02-17 21:55
Status: 200
Content-Type: text/html; charset=UTF-8
Size: 2 B
```
### Handling cookies using `res_handlers`
`res_handlers` can be used to set cookies, by adding a `Set-Cookie` header, using both the `set_cookie()` and `remove_cookie()` functions.
Note that you can get them from the server with `get_cookies()`, and parse the cookie string using `parse_cookie_string`.
```{r eval = TRUE}
parse_cookie_string("a=12;session=blabla")
```
In the example, we'll also use `brochure::server_redirect("/")` to redirect the user after login.
```{r}
# Creating a navlink
nav_links <- tags$ul(
tags$li(
tags$a(href = "/", "home"),
),
tags$li(
tags$a(href = "/login", "login"),
),
tags$li(
tags$a(href = "/logout", "logout"),
)
)
home <- function() {
page(
href = "/",
ui = tagList(
h1("This is my first page"),
tags$p("It will contain BROCHURECOOKIE depending on the last page you've visited (/login or /logout)"),
verbatimTextOutput("cookie"),
nav_links
),
server = function(input, output, session) {
output$cookie <- renderPrint({
parse_cookie_string(
get_cookies()
)
})
}
)
}
login <- function() {
page(
href = "/login",
ui = tagList(
h1("You've just logged!"),
verbatimTextOutput("cookie"),
actionButton("redirect", "Redirect to the home page"),
nav_links
),
server = function(input, output, session) {
output$cookie <- renderPrint({
parse_cookie_string(
get_cookies()
)
})
observeEvent(input$redirect, {
# Using brochure to redirect to another page
server_redirect("/")
})
},
res_handlers = list(
# We'll add a cookie here
~ set_cookie(.x, "BROCHURECOOKIE", 12)
# If you had to do it yourself
# function(res, req){
# res$headers$`Set-Cookie` <- "BROCHURECOOKIE=12; HttpOnly;"
# res
# }
)
)
}
logout <- function() {
page(
href = "/logout",
ui = tagList(
h1("You've logged out"),
nav_links,
verbatimTextOutput("cookie")
),
server = function(input, output, session) {
output$cookie <- renderPrint({
parse_cookie_string(
get_cookies()
)
})
},
res_handlers = list(
# We'll remove the cookie here
~ remove_cookie(.x, "BROCHURECOOKIE")
# If you had to do it yourself
# function(res, req){
# res$headers$`Set-Cookie` <- "BROCHURECOOKIE=''; Max-Age = 0;"
# res
# }
)
)
}
brochureApp(
# Pages
home(),
login(),
logout()
)
```
## Design pattern
Note that every time you open a new page, a __new shiny session is launched__.
This is different from what you usually do when you are building a `{shiny}` app that works as a single page application.
This is no longer the case in `{brochure}`.
What that means is that there is no data persistence in R when navigating from one page to the other.
That might seem like a downside, but I believe that it will actually be for the best: it will make developers think more carefully about the data flow of their application.
That being said, how do we keep track of a user though pages, so that if they do something in a page, it's reflected on another?
To do that, you'd need to add a form of session identifier, like a cookie: this can for example be done using the [`{glouton}`](https://github.com/colinfay/glouton) package if you want to manage it with JS.
You can also use the cookie example from before.
You'll also need a form of backend storage (here in the example, we use [`{cachem}`](https://github.com/r-lib/cachem), but you can also use an external DB like SQLite or MongoDB).
```{r}
library(glouton)
# Creating a storage system
cache_system <- cachem::cache_disk(tempdir())
nav_links <- tags$ul(
tags$li(
tags$a(href = "/", "home"),
),
tags$li(
tags$a(href = "/page2", "page2"),
)
)
cookie_set <- function() {
r <- reactiveValues()
observeEvent(
TRUE,
{
# Fetch the cookies using {glouton}
r$cook <- fetch_cookies()
# If there is no stored cookie for {brochure}, we generate it
if (is.null(r$cook$brochure_cookie)) {
# Generate a random id
session_id <- digest::sha1(paste(Sys.time(), sample(letters, 16)))
# Add this id as a cookie
add_cookie("brochure_cookie", session_id)
# Store in in the reactiveValues list
r$cook$brochure_cookie <- session_id
}
# For debugging purpose
print(r$cook$brochure_cookie)
},
once = TRUE
)
return(r)
}
page_1 <- function() {
page(
href = "/",
ui = tagList(
h1("This is my first page"),
nav_links,
# The text enter on page 1 will be available on page 2, using
# a session cookie and a storage system
textInput("textenter", "Enter a text"),
actionButton("save", "Save my text and go to page2")
),
server = function(input, output, session) {
r <- cookie_set()
observeEvent(input$save, {
# Use the session id to save on the cache system
cache_system$set(
paste0(
r$cook$brochure_cookie,
"text"
),
input$textenter
)
server_redirect("/page2")
})
}
)
}
page_2 <- function() {
page(
href = "/page2",
ui = tagList(
h1("This is my second page"),
nav_links,
# The text enter on page 1 will be available here, reading
# the storage system
verbatimTextOutput("textdisplay")
),
server = function(input, output, session) {
r <- cookie_set()
output$textdisplay <- renderPrint({
# Getting the content value based on the session cookie
cache_system$get(
paste0(
r$cook$brochure_cookie,
"text"
)
)
})
}
)
}
brochureApp(
# Setting {glouton} globally
use_glouton(),
# Pages
page_1(),
page_2()
# Redirections
)
```
## With `{golem}`
### Fresh `{golem}` App
You can set up a `{brochure}` based app with `{golem}` using the `brochure::golem_hook()` function.
```r
golem::create_golem("mapmyrace", project_hook = brochure::golem_hook)
```
You can also use the module_template function to create a `{brochure}` module :
```r
golem::add_module(name = "pouet", module_template = brochure::new_page)
```
### Adapt old app
To adapt your `{golem}` based application to `{brochure}`, here are the two steps to follow:
+ Remove the app_server.R file, and the top of app_ui => You'll still need `golem_add_external_resources()`.
+ Build the pages inside separate R scripts, following the example from this `README`.
```
.
├── DESCRIPTION
├── NAMESPACE
├── R
│ ├── app_config.R
│ ├── home.R ### YOUR PAGE
│ ├── login.R ### YOUR PAGE
│ ├── logout.R ### YOUR PAGE
│ └── run_app.R ### YOUR PAGE
├── dev
│ ├── 01_start.R
│ ├── 02_dev.R
│ ├── 03_deploy.R
│ └── run_dev.R
├── inst
│ ├── app
│ │ └── www
│ │ ├── favicon.ico
│ └── golem-config.yml
├── man
│ └── run_app.Rd
```
+ Replace `shinyApp` with `brochureApp` in `run_app()`, add the external resources, then your pages.
```{r}
run_app <- function(
onStart = NULL,
options = list(),
enableBookmarking = NULL,
...
) {
with_golem_options(
app = brochureApp(
# Putting the resources here
golem_add_external_resources(),
home(),
login(),
logout(),
onStart = onStart,
options = options,
enableBookmarking = enableBookmarking
),
golem_opts = list(...)
)
}
```
## Previous work
Other packages that implements features that are closed to what `{brochure}` does:
+ [`{shiny.router}`](https://appsilon.com/shiny-router-020/)
+ [`{blaze}`](https://github.com/nteetor/blaze)
As far as I can tell, these packages doesn't serve the same goal as what `{brochure}` does, as they both still serve Single Page Applications.