11.2 Modifying app to be bookmarkable
There are three main parts you must modify in a Shiny app in order for it to be bookmarkable:
- In the
ui
: make it a function
- In the
ui
: add thebookmarkButton()
function
- In the
shinyApp()
call: includeenableBookmarking = "url"
ui <- function(request) {
fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput("omega", "omega", value = 1, min = -2, max = 2, step = 0.01),
sliderInput("delta", "delta", value = 1, min = 0, max = 2, step = 0.01),
sliderInput("damping", "damping", value = 1, min = 0.9, max = 1, step = 0.001),
numericInput("length", "length", value = 100),
bookmarkButton()
),
mainPanel(
plotOutput("fig")
)
)
)
}
shinyApp(ui, server, enableBookmarking = "url")
- Making the
ui
a function- Why? Because Shiny needs to be able to modify the input controls specified by the URL. Like arguments are passed within a normal R function as parameters to produce different outputs, the
ui
now needs to take in an argument (the URL) in order to return the app into a particular state. The URL holds information on the input parameters that needs to be changed, hence why theui
now needs to be a function.
- Why? Because Shiny needs to be able to modify the input controls specified by the URL. Like arguments are passed within a normal R function as parameters to produce different outputs, the
- Adding the
bookmarkButton()
button to the app- This function adds a button to the
ui
that captures the current values of all the input controls and generates a URL from it. More on this later.
- This function adds a button to the
- Adding
enableBookmarking = "url"
to theshinyApp()
call- This function ultimately puts the app together - the
ui
and theserver
. Adding this argument tells Shiny enable bookmarking and that the bookmark will be URL-encoded vs saved-on-server. More on this later
- This function ultimately puts the app together - the