Debian Codegraph @

man/enableBookmarking.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/bookmark-state.R
3 \name{enableBookmarking}
4 \alias{enableBookmarking}
5 \title{Enable bookmarking for a Shiny application}
6 \usage{
7 enableBookmarking(store = c("url", "server", "disable"))
8 }
9 \arguments{
10 \item{store}{Either \code{"url"}, which encodes all of the relevant values in
11 a URL, \code{"server"}, which saves to disk on the server, or
12 \code{"disable"}, which disables any previously-enabled bookmarking.}
13 }
14 \description{
15 There are two types of bookmarking: saving an application's state to disk on
16 the server, and encoding the application's state in a URL. For state that has
17 been saved to disk, the state can be restored with the corresponding state
18 ID. For URL-encoded state, the state of the application is encoded in the
19 URL, and no server-side storage is needed.
20
21 URL-encoded bookmarking is appropriate for applications where there not many
22 input values that need to be recorded. Some browsers have a length limit for
23 URLs of about 2000 characters, and if there are many inputs, the length of
24 the URL can exceed that limit.
25
26 Saved-on-server bookmarking is appropriate when there are many inputs, or
27 when the bookmarked state requires storing files.
28 }
29 \details{
30 For restoring state to work properly, the UI must be a function that takes
31 one argument, \code{request}. In most Shiny applications, the UI is not a
32 function; it might have the form \code{fluidPage(....)}. Converting it to a
33 function is as simple as wrapping it in a function, as in
34 \code{function(request) \{ fluidPage(....) \}}.
35
36 By default, all input values will be bookmarked, except for the values of
37 passwordInputs. fileInputs will be saved if the state is saved on a server,
38 but not if the state is encoded in a URL.
39
40 When bookmarking state, arbitrary values can be stored, by passing a function
41 as the \code{onBookmark} argument. That function will be passed a
42 \code{ShinySaveState} object. The \code{values} field of the object is a list
43 which can be manipulated to save extra information. Additionally, if the
44 state is being saved on the server, and the \code{dir} field of that object
45 can be used to save extra information to files in that directory.
46
47 For saved-to-server state, this is how the state directory is chosen:
48 \itemize{
49 \item If running in a hosting environment such as Shiny Server or
50 Connect, the hosting environment will choose the directory.
51 \item If running an app in a directory with \code{\link[=runApp]{runApp()}}, the
52 saved states will be saved in a subdirectory of the app called
53 shiny_bookmarks.
54 \item If running a Shiny app object that is generated from code (not run
55 from a directory), the saved states will be saved in a subdirectory of
56 the current working directory called shiny_bookmarks.
57 }
58
59 When used with \code{\link[=shinyApp]{shinyApp()}}, this function must be called before
60 \code{shinyApp()}, or in the \code{shinyApp()}'s \code{onStart} function. An
61 alternative to calling the \code{enableBookmarking()} function is to use the
62 \code{enableBookmarking} \emph{argument} for \code{shinyApp()}. See examples
63 below.
64 }
65 \examples{
66 ## Only run these examples in interactive R sessions
67 if (interactive()) {
68
69 # Basic example with state encoded in URL
70 ui <- function(request) {
71 fluidPage(
72 textInput("txt", "Text"),
73 checkboxInput("chk", "Checkbox"),
74 bookmarkButton()
75 )
76 }
77 server <- function(input, output, session) { }
78 enableBookmarking("url")
79 shinyApp(ui, server)
80
81
82 # An alternative to calling enableBookmarking(): use shinyApp's
83 # enableBookmarking argument
84 shinyApp(ui, server, enableBookmarking = "url")
85
86
87 # Same basic example with state saved to disk
88 enableBookmarking("server")
89 shinyApp(ui, server)
90
91
92 # Save/restore arbitrary values
93 ui <- function(req) {
94 fluidPage(
95 textInput("txt", "Text"),
96 checkboxInput("chk", "Checkbox"),
97 bookmarkButton(),
98 br(),
99 textOutput("lastSaved")
100 )
101 }
102 server <- function(input, output, session) {
103 vals <- reactiveValues(savedTime = NULL)
104 output$lastSaved <- renderText({
105 if (!is.null(vals$savedTime))
106 paste("Last saved at", vals$savedTime)
107 else
108 ""
109 })
110
111 onBookmark(function(state) {
112 vals$savedTime <- Sys.time()
113 # state is a mutable reference object, and we can add arbitrary values
114 # to it.
115 state$values$time <- vals$savedTime
116 })
117 onRestore(function(state) {
118 vals$savedTime <- state$values$time
119 })
120 }
121 enableBookmarking(store = "url")
122 shinyApp(ui, server)
123
124
125 # Usable with dynamic UI (set the slider, then change the text input,
126 # click the bookmark button)
127 ui <- function(request) {
128 fluidPage(
129 sliderInput("slider", "Slider", 1, 100, 50),
130 uiOutput("ui"),
131 bookmarkButton()
132 )
133 }
134 server <- function(input, output, session) {
135 output$ui <- renderUI({
136 textInput("txt", "Text", input$slider)
137 })
138 }
139 enableBookmarking("url")
140 shinyApp(ui, server)
141
142
143 # Exclude specific inputs (The only input that will be saved in this
144 # example is chk)
145 ui <- function(request) {
146 fluidPage(
147 passwordInput("pw", "Password"), # Passwords are never saved
148 sliderInput("slider", "Slider", 1, 100, 50), # Manually excluded below
149 checkboxInput("chk", "Checkbox"),
150 bookmarkButton()
151 )
152 }
153 server <- function(input, output, session) {
154 setBookmarkExclude("slider")
155 }
156 enableBookmarking("url")
157 shinyApp(ui, server)
158
159
160 # Update the browser's location bar every time an input changes. This should
161 # not be used with enableBookmarking("server"), because that would create a
162 # new saved state on disk every time the user changes an input.
163 ui <- function(req) {
164 fluidPage(
165 textInput("txt", "Text"),
166 checkboxInput("chk", "Checkbox")
167 )
168 }
169 server <- function(input, output, session) {
170 observe({
171 # Trigger this observer every time an input changes
172 reactiveValuesToList(input)
173 session$doBookmark()
174 })
175 onBookmarked(function(url) {
176 updateQueryString(url)
177 })
178 }
179 enableBookmarking("url")
180 shinyApp(ui, server)
181
182
183 # Save/restore uploaded files
184 ui <- function(request) {
185 fluidPage(
186 sidebarLayout(
187 sidebarPanel(
188 fileInput("file1", "Choose CSV File", multiple = TRUE,
189 accept = c(
190 "text/csv",
191 "text/comma-separated-values,text/plain",
192 ".csv"
193 )
194 ),
195 tags$hr(),
196 checkboxInput("header", "Header", TRUE),
197 bookmarkButton()
198 ),
199 mainPanel(
200 tableOutput("contents")
201 )
202 )
203 )
204 }
205 server <- function(input, output) {
206 output$contents <- renderTable({
207 inFile <- input$file1
208 if (is.null(inFile))
209 return(NULL)
210
211 if (nrow(inFile) == 1) {
212 read.csv(inFile$datapath, header = input$header)
213 } else {
214 data.frame(x = "multiple files")
215 }
216 })
217 }
218 enableBookmarking("server")
219 shinyApp(ui, server)
220
221 }
222 }
223 \seealso{
224 \code{\link[=onBookmark]{onBookmark()}}, \code{\link[=onBookmarked]{onBookmarked()}},
225 \code{\link[=onRestore]{onRestore()}}, and \code{\link[=onRestored]{onRestored()}} for registering
226 callback functions that are invoked when the state is bookmarked or
227 restored.
228
229 Also see \code{\link[=updateQueryString]{updateQueryString()}}.
230 }