Debian Codegraph @

man/onBookmark.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/bookmark-state.R
3 \name{onBookmark}
4 \alias{onBookmark}
5 \alias{onBookmarked}
6 \alias{onRestore}
7 \alias{onRestored}
8 \title{Add callbacks for Shiny session bookmarking events}
9 \usage{
10 onBookmark(fun, session = getDefaultReactiveDomain())
11
12 onBookmarked(fun, session = getDefaultReactiveDomain())
13
14 onRestore(fun, session = getDefaultReactiveDomain())
15
16 onRestored(fun, session = getDefaultReactiveDomain())
17 }
18 \arguments{
19 \item{fun}{A callback function which takes one argument.}
20
21 \item{session}{A shiny session object.}
22 }
23 \description{
24 These functions are for registering callbacks on Shiny session events. They
25 should be called within an application's server function.
26
27 \itemize{
28 \item \code{onBookmark} registers a function that will be called just
29 before Shiny bookmarks state.
30 \item \code{onBookmarked} registers a function that will be called just
31 after Shiny bookmarks state.
32 \item \code{onRestore} registers a function that will be called when a
33 session is restored, after the server function executes, but before all
34 other reactives, observers and render functions are run.
35 \item \code{onRestored} registers a function that will be called after a
36 session is restored. This is similar to \code{onRestore}, but it will be
37 called after all reactives, observers, and render functions run, and
38 after results are sent to the client browser. \code{onRestored}
39 callbacks can be useful for sending update messages to the client
40 browser.
41 }
42 }
43 \details{
44 All of these functions return a function which can be called with no
45 arguments to cancel the registration.
46
47 The callback function that is passed to these functions should take one
48 argument, typically named "state" (for \code{onBookmark}, \code{onRestore},
49 and \code{onRestored}) or "url" (for \code{onBookmarked}).
50
51 For \code{onBookmark}, the state object has three relevant fields. The
52 \code{values} field is an environment which can be used to save arbitrary
53 values (see examples). If the state is being saved to disk (as opposed to
54 being encoded in a URL), the \code{dir} field contains the name of a
55 directory which can be used to store extra files. Finally, the state object
56 has an \code{input} field, which is simply the application's \code{input}
57 object. It can be read, but not modified.
58
59 For \code{onRestore} and \code{onRestored}, the state object is a list. This
60 list contains \code{input}, which is a named list of input values to restore,
61 \code{values}, which is an environment containing arbitrary values that were
62 saved in \code{onBookmark}, and \code{dir}, the name of the directory that
63 the state is being restored from, and which could have been used to save
64 extra files.
65
66 For \code{onBookmarked}, the callback function receives a string with the
67 bookmark URL. This callback function should be used to display UI in the
68 client browser with the bookmark URL. If no callback function is registered,
69 then Shiny will by default display a modal dialog with the bookmark URL.
70 }
71 \section{Modules}{
72
73
74 These callbacks may also be used in Shiny modules. When used this way, the
75 inputs and values will automatically be namespaced for the module, and the
76 callback functions registered for the module will only be able to see the
77 module's inputs and values.
78 }
79
80 \examples{
81 ## Only run these examples in interactive sessions
82 if (interactive()) {
83
84 # Basic use of onBookmark and onRestore: This app saves the time in its
85 # arbitrary values, and restores that time when the app is restored.
86 ui <- function(req) {
87 fluidPage(
88 textInput("txt", "Input text"),
89 bookmarkButton()
90 )
91 }
92 server <- function(input, output) {
93 onBookmark(function(state) {
94 savedTime <- as.character(Sys.time())
95 cat("Last saved at", savedTime, "\n")
96 # state is a mutable reference object, and we can add arbitrary values to
97 # it.
98 state$values$time <- savedTime
99 })
100
101 onRestore(function(state) {
102 cat("Restoring from state bookmarked at", state$values$time, "\n")
103 })
104 }
105 enableBookmarking("url")
106 shinyApp(ui, server)
107
108
109
110 ui <- function(req) {
111 fluidPage(
112 textInput("txt", "Input text"),
113 bookmarkButton()
114 )
115 }
116 server <- function(input, output, session) {
117 lastUpdateTime <- NULL
118
119 observeEvent(input$txt, {
120 updateTextInput(session, "txt",
121 label = paste0("Input text (Changed ", as.character(Sys.time()), ")")
122 )
123 })
124
125 onBookmark(function(state) {
126 # Save content to a file
127 messageFile <- file.path(state$dir, "message.txt")
128 cat(as.character(Sys.time()), file = messageFile)
129 })
130
131 onRestored(function(state) {
132 # Read the file
133 messageFile <- file.path(state$dir, "message.txt")
134 timeText <- readChar(messageFile, 1000)
135
136 # updateTextInput must be called in onRestored, as opposed to onRestore,
137 # because onRestored happens after the client browser is ready.
138 updateTextInput(session, "txt",
139 label = paste0("Input text (Changed ", timeText, ")")
140 )
141 })
142 }
143 # "server" bookmarking is needed for writing to disk.
144 enableBookmarking("server")
145 shinyApp(ui, server)
146
147
148 # This app has a module, and both the module and the main app code have
149 # onBookmark and onRestore functions which write and read state$values$hash. The
150 # module's version of state$values$hash does not conflict with the app's version
151 # of state$values$hash.
152 #
153 # A basic module that captializes text.
154 capitalizerUI <- function(id) {
155 ns <- NS(id)
156 wellPanel(
157 h4("Text captializer module"),
158 textInput(ns("text"), "Enter text:"),
159 verbatimTextOutput(ns("out"))
160 )
161 }
162 capitalizerServer <- function(input, output, session) {
163 output$out <- renderText({
164 toupper(input$text)
165 })
166 onBookmark(function(state) {
167 state$values$hash <- rlang::hash(input$text)
168 })
169 onRestore(function(state) {
170 if (identical(rlang::hash(input$text), state$values$hash)) {
171 message("Module's input text matches hash ", state$values$hash)
172 } else {
173 message("Module's input text does not match hash ", state$values$hash)
174 }
175 })
176 }
177 # Main app code
178 ui <- function(request) {
179 fluidPage(
180 sidebarLayout(
181 sidebarPanel(
182 capitalizerUI("tc"),
183 textInput("text", "Enter text (not in module):"),
184 bookmarkButton()
185 ),
186 mainPanel()
187 )
188 )
189 }
190 server <- function(input, output, session) {
191 callModule(capitalizerServer, "tc")
192 onBookmark(function(state) {
193 state$values$hash <- rlang::hash(input$text)
194 })
195 onRestore(function(state) {
196 if (identical(rlang::hash(input$text), state$values$hash)) {
197 message("App's input text matches hash ", state$values$hash)
198 } else {
199 message("App's input text does not match hash ", state$values$hash)
200 }
201 })
202 }
203 enableBookmarking(store = "url")
204 shinyApp(ui, server)
205 }
206 }
207 \seealso{
208 enableBookmarking for general information on bookmarking.
209 }