shinystate
is an R package that provides additional customization on top of the standard Shiny bookmarkable state capabilities.
You can install the development version from GitHub with the remotes package:
remotes::install_github("rpodcast/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:
- Flexible configuration of where bookmarkable state files are stored, whether on the same file system as the server running the application, or in a separate repository such as cloud storage.
- Allow users to save multiple bookmarkable state sessions, tailored to situations such as multiple "projects" inside the same application.
- Augment the bookmarkable state artifacts with metadata of your choosing. Possible metadata could include custom names and timestamps.
The shinystate
package offers an intuitive class system built upon the R6
package with methods tailored to the common operations with managing bookmarkable state.
Here is the general setup procedure for incorporating shinystate
in your Shiny application:
- Load the package in your application with
library(shinystate)
or other methods used in frameworks such asgolem
orrhino
. - Create a new storage class object in the beginning of your application with
StorageClass$new()
. Optional parameters exist and are discussed in the detailed user guides. - Inside the user interface function of your application, add
use_shinystate()
. - Inside the server function of your application, execute the
register_metadata()
method. - Add a call to
shiny::enableBookmarking("server")
either in the server function or as part of a custom function used for theonStart
parameter inshiny::shinyApp()
.
Once the setup is complete, you can use the following methods with the storage object to perform common operations:
snapshot()
: Save the state of the application as a set of bookmarkable state files.restore(url)
: Restore a previously-saved state based on the unique URL of the snapshot
Here is an example Shiny application inspired by the single-file example from the official introduction to bookmarking state article that utilizes shinystate
:
library(shiny)
library(shinystate)
storage <- StorageClass$new()
ui <- function(request) {
fluidPage(
use_shinystate(),
textInput("txt", "Enter text"),
checkboxInput("caps", "Capitalize"),
verbatimTextOutput("out"),
actionButton("bookmark", "Bookmark"),
actionButton("restore", "Restore Last Bookmark")
)
}
server <- function(input, output, session) {
storage$register_metadata()
output$out <- renderText({
if (input$caps) {
toupper(input$txt)
} else {
input$txt
}
})
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("bookmark", "restore"))
}
shinyApp(ui, server, onStart = function() {
shiny::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.