Skip to content

Commit 0296f40

Browse files
authored
Merge pull request #294 from Appsilon/support-bookmarking
Support bookmarking
2 parents 116d17b + 9339f1c commit 0296f40

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

R/app.R

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,18 @@ load_main_module <- function() {
4242
}
4343

4444
as_top_level <- function(shiny_module) {
45+
# Necessary to avoid infinite recursion / bugs due to lazy evaluation:
46+
# https://adv-r.hadley.nz/function-factories.html?q=force#forcing-evaluation
47+
force(shiny_module)
48+
4549
list(
46-
ui = shiny_module$ui("app"),
50+
# Wrap the UI in a function to support Shiny bookmarking.
51+
ui = function(request) shiny_module$ui("app"),
4752
server = function(input, output) shiny_module$server("app")
4853
)
4954
}
5055

51-
with_head_tags <- function(ui) {
56+
attach_head_tags <- function(ui) {
5257
shiny::tagList(
5358
shiny::tags$head(
5459
shiny::tags$script(src = "static/js/app.min.js"),
@@ -59,6 +64,12 @@ with_head_tags <- function(ui) {
5964
)
6065
}
6166

67+
with_head_tags <- function(ui) {
68+
# The top-level UI must be a function for Shiny bookmarking to work.
69+
if (is.function(ui)) function(request) attach_head_tags(ui(request))
70+
else attach_head_tags(ui)
71+
}
72+
6273
#' Rhino application
6374
#'
6475
#' The entrypoint for a Rhino application.

pkgdown/_pkgdown.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ navbar:
4747
href: articles/how-to/manage-r-dependencies.html
4848
- text: Manage secrets and environments
4949
href: articles/how-to/manage-secrets-and-environments.html
50+
- text: Enable Shiny bookmarking
51+
href: articles/how-to/enable-shiny-bookmarking.html
5052
- text: Keep multiple apps in a single repository
5153
href: articles/how-to/keep-multiple-apps-in-a-single-repository.html
5254
- text: Use Rhino on Windows
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
title: "How-to: Enable Shiny bookmarking"
3+
output: rmarkdown::html_vignette
4+
vignette: >
5+
%\VignetteIndexEntry{How-to: Enable Shiny bookmarking}
6+
%\VignetteEngine{knitr::rmarkdown}
7+
%\VignetteEncoding{UTF-8}
8+
---
9+
10+
To use Shiny [bookmarking](https://shiny.rstudio.com/articles/bookmarking-state.html),
11+
call `shiny::enableBookmarking()` somewhere in your `main.R`:
12+
13+
```r
14+
box::use(
15+
shiny,
16+
)
17+
18+
shiny$enableBookmarking()
19+
20+
#' @export
21+
ui <- function(id) {
22+
ns <- shiny$NS(id)
23+
shiny$bootstrapPage(
24+
shiny$bookmarkButton(),
25+
shiny$textInput(ns("name"), "Name"),
26+
shiny$textOutput(ns("message"))
27+
)
28+
}
29+
30+
#' @export
31+
server <- function(id) {
32+
shiny$moduleServer(id, function(input, output, session) {
33+
output$message <- shiny$renderText(paste0("Hello ", input$name, "!"))
34+
})
35+
}
36+
```
37+
38+
If you are using a [legacy entrypoint](https://appsilon.github.io/rhino/reference/app.html#legacy-entrypoint),
39+
be sure to make your UI a function
40+
as described in the details section of `shiny::enableBookmarking()`.

0 commit comments

Comments
 (0)