Enhance the bookmarkable state feature of 'shiny' with additional customization such as storage location and storage repositories leveraging the 'pins' package.

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:
remotes::install_github("rpodcast/shinystate")
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)
storage <- StorageClass$new()
ui <- function(request) {
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")
)
)
}
server <- function(input, output, session) {
storage$register_metadata()
plot_title <- reactive({
if (!shiny::isTruthy(input$txt)) {
value <- "Default Title"
} else {
value <- input$txt
}
if (input$caps) {
value <- toupper(value)
}
return(value)
})
output$distPlot <- renderPlot({
req(plot_title())
x <- faithful$waiting
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(
x,
breaks = bins,
col = "#007bc2",
border = "white",
xlab = "Waiting time to next eruption (in mins)",
main = plot_title()
)
})
observeEvent(input$bookmark, {
storage$snapshot()
showNotification("Session successfully saved")
})
observeEvent(input$restore, {
session_df <- storage$get_sessions()
storage$restore(tail(session_df$url, n = 1))
})
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.