shinystate
is an R package that provides additional
customization on top of the standard Shiny bookmarkable
state capabilities.
# Install the released version from CRAN
install.packages("shinystate")
# Or the development version from GitHub:
::install_github("rpodcast/shinystate") remotes
shinystate
?If your Shiny application leverages bookmarkable state and the
default feature set is working for your use case, then
shinystate
is likely not value-added.
However, as applications grow in complexity and are used in high-stakes situations, you may wish your application could support the following features:
The shinystate
package offers an intuitive class system
built upon the R6
package with methods tailored to the
common operations with managing bookmarkable state.
To enable saving bookmarkable state with shinystate
, you
need to:
library(shinystate)
StorageClass
class outside of
the application user interface and server functions:
StorageClass$new()
use_shinystate()
in your UI definitionregister_metadata()
method from your instance
of the StorageClass
class at the beginning of the
application server functionenableBookmarking = 'server'
in the call to
shinyApp()
snapshot()
method from your instance of the
StorageClass
class to save the state of the Shiny app
sessionrestore()
method from your instance of the
StorageClass
class to restore a saved session based on the
session URL, available in the data frame returned from the
get_sessions()
method.Below is an example application illustrating the default usage of
shinystate
. Visit the Getting
Started for additional details.
library(shiny)
library(bslib)
library(shinystate)
<- StorageClass$new()
storage
<- function(request) {
ui page_sidebar(
title = "Basic App",
sidebar = sidebar(
accordion(
open = TRUE,
accordion_panel(
id = "user_inputs",
"User Inputs",
textInput(
"txt",
label = "Enter Title",
placeholder = "change this"
),checkboxInput("caps", "Capitalize"),
sliderInput(
"bins",
label = "Number of bins",
min = 1,
max = 50,
value = 30
)
),accordion_panel(
id = "state",
"Bookmark State",
actionButton("bookmark", "Bookmark"),
actionButton("restore", "Restore Last Bookmark")
)
)
),use_shinystate(),
card(
card_header("App Output"),
plotOutput("distPlot")
)
)
}
<- function(input, output, session) {
server $register_metadata()
storage
<- reactive({
plot_title if (!shiny::isTruthy(input$txt)) {
<- "Default Title"
value else {
} <- input$txt
value
}
if (input$caps) {
<- toupper(value)
value
}
return(value)
})
$distPlot <- renderPlot({
outputreq(plot_title())
<- faithful$waiting
x <- seq(min(x), max(x), length.out = input$bins + 1)
bins hist(
x,breaks = bins,
col = "#007bc2",
border = "white",
xlab = "Waiting time to next eruption (in mins)",
main = plot_title()
)
})
observeEvent(input$bookmark, {
$snapshot()
storageshowNotification("Session successfully saved")
})
observeEvent(input$restore, {
<- storage$get_sessions()
session_df $restore(tail(session_df$url, n = 1))
storage
})
setBookmarkExclude(c("add", "bookmark", "restore"))
}
shinyApp(ui, server, enableBookmarking = "server")
Please note that the shinystate project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.