| Title: | Customization of Shiny Bookmarkable State |
|---|---|
| Description: | Enhance the bookmarkable state feature of 'shiny' with additional customization such as storage location and storage repositories leveraging the 'pins' package. |
| Authors: | Eric Nantz [aut, cre] (ORCID: <https://orcid.org/0000-0001-8104-7510>), John Brothers [ctb], Eli Lilly and Company [cph, fnd] |
| Maintainer: | Eric Nantz <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0.9002 |
| Built: | 2026-05-12 08:36:58 UTC |
| Source: | https://github.com/rpodcast/shinystate |
The saveInterfaceLocal and loadInterfaceLocal functions provide
implementations for saving and loading Shiny bookmark state to a
local directory. These functions can be set as the saving and
loading interfaces for Shiny bookmarking using
shiny::shinyOptions().. While these callback functions are
set by default when initializing a new instance of StorageClass,
certain Shiny application structures such as applications created
with the golem R package require these callbacks to be defined
as part of the onStart argument in shiny::runApp().
saveInterfaceLocal(id, callback) loadInterfaceLocal(id, callback)saveInterfaceLocal(id, callback) loadInterfaceLocal(id, callback)
id |
character string for session ID. |
callback |
function to call with the path to save/load the bookmark state. |
This class provides a set of methods to create and manage Shiny bookmarkable state files.
Session metadata is stored directly in each bookmark pin's metadata field rather than in a separate shared "sessions" pin. This approach eliminates race conditions from concurrent read-modify-write operations and leverages pins' built-in local caching for fast repeated reads.
local_storage_dirfile path to use for storing bookmarkable state files. If not specified, a temporary directory on the host system will be used.
board_sessionsOptional pre-created board object created with the
pins package. If missing, a folder-based pin board will be created using
the local_storage_dir path.
new()
Initialize a StorageClass object
StorageClass$new(local_storage_dir = NULL, board_sessions = NULL)
local_storage_dirfile path to use for storing bookmarkable state files. If not specified, a temporary directory on the host system will be used.
board_sessionsOptional pre-created board object created with the
pins package. If missing, a folder-based pin board will be created using
the local_storage_dir path.
An object with class StorageClass and the methods described
in this documentation
## Only run examples in interactive R sessions
if (interactive()) {
# beginning of application
library(shiny)
library(shinystate)
# Create a StorageClass object with default settings
storage <- StorageClass$new()
# Use a pre-specified directory to store state files
# For purposes of this example, use a temporary directory
storage <- StorageClass$new(local_storage_dir = tempdir())
# use a custom pins board to store bookmarkable state data
# For purposes of this example, use a temporary directory
library(pins)
board <- board_temp()
storage <- StorageClass$new(board_sessions = board)
}
get_sessions()
Obtain saved bookmarkable state session metadata
Calls $get_sessions() on the StorageClass object to extract
the bookmarkable state session metadata. You can leverage this data
frame in your Shiny application to let the user manage their existing
bookmarkable state sessions, for example.
StorageClass$get_sessions()
data frame of bookmarkable session metadata if at least one
bookmarkable state session has been saved. Otherwise, the return
object will be NULL.
## Only run examples in interactive R sessions
if (interactive()) {
library(shiny)
library(shinystate)
# Create a StorageClass object with default settings
storage <- StorageClass$new()
# obtain session data
storage$get_sessions()
}
reactive_sessions()
Create a reactive sessions data.frame for Shiny
Returns a reactive expression that refreshes the sessions list when the provided trigger reactive changes. Useful for refreshing after saves or when a modal opens.
StorageClass$reactive_sessions(trigger = NULL)
triggerA reactive expression that triggers refresh when changed.
A reactive expression returning sessions data.frame
## Only run examples in interactive R sessions
if (interactive()) {
library(shiny)
library(shinystate)
storage <- StorageClass$new()
server <- function(input, output, session) {
refresh_trigger <- reactiveVal(0)
session_df <- storage$reactive_sessions(
trigger = reactive(list(refresh_trigger(), input$show_load_modal))
)
observeEvent(input$save, {
storage$snapshot(session_metadata = list(name = input$save_name))
refresh_trigger(refresh_trigger() + 1)
})
}
}
restore()
Restore a previous bookmarkable state session
StorageClass$restore(url, session = shiny::getDefaultReactiveDomain())
urlcharacter with the unique URL assigned to the bookmarkable state session.
sessionThe Shiny session to associate with the restore operation
## Only run examples in interactive R sessions
if (interactive()) {
library(shinystate)
# Create a StorageClass object with default settings
storage <- StorageClass$new()
# obtain session data
session_df <- storage$get_sessions()
# restore state
# typically run inside a shiny observe or observeEvent call
storage$restore(tail(session_df$url, n = 1))
}
snapshot()
Create a snapshot of bookmarkable state
StorageClass$snapshot( session_metadata = NULL, session = shiny::getDefaultReactiveDomain() )
session_metadataOptional named list of additional variables to include with the default bookmarkable state attributes when creating the snapshot. Each element of the list must be a single-length item
sessionThe Shiny session to associate with the snapshot operation
## Only run examples in interactive R sessions
if (interactive()) {
library(shinystate)
# Create a StorageClass object with default settings
storage <- StorageClass$new()
# save state with timestamp as metadata
# typically run inside a shiny observe or observeEvent call
storage$snapshot(session_metadata = list(time = Sys.time()))
}
delete()
Delete a previous snapshot of bookmarkable state
StorageClass$delete(url)
urlcharacter with the unique URL assigned to the bookmarkable state session.
## Only run examples in interactive R sessions
if (interactive()) {
library(shinystate)
# Create a StorageClass object with default settings
storage <- StorageClass$new()
# obtain session data
session_df <- storage$get_sessions()
# delete a session
# typically run inside a shiny observe or observeEvent call
storage$delete(session_df$url[1])
}
register_metadata()
Register bookmarkable state storage data collection
This method must be called in the application server function to perform the necessary customizations to bookmarkable state methods. This function is meant to be called near the beginning of the Shiny application server function.
StorageClass$register_metadata()
## Only run examples in interactive R sessions
if (interactive()) {
library(shinystate)
# Create a StorageClass object with default settings
storage <- StorageClass$new()
# application server code
server <- function(input, output, session) {
storage$register_metadata()
}
}
clone()
The objects of this class are cloneable with this method.
StorageClass$clone(deep = FALSE)
deepWhether to make a deep clone.
## ------------------------------------------------ ## Method `StorageClass$new` ## ------------------------------------------------ ## Only run examples in interactive R sessions if (interactive()) { # beginning of application library(shiny) library(shinystate) # Create a StorageClass object with default settings storage <- StorageClass$new() # Use a pre-specified directory to store state files # For purposes of this example, use a temporary directory storage <- StorageClass$new(local_storage_dir = tempdir()) # use a custom pins board to store bookmarkable state data # For purposes of this example, use a temporary directory library(pins) board <- board_temp() storage <- StorageClass$new(board_sessions = board) } ## ------------------------------------------------ ## Method `StorageClass$get_sessions` ## ------------------------------------------------ ## Only run examples in interactive R sessions if (interactive()) { library(shiny) library(shinystate) # Create a StorageClass object with default settings storage <- StorageClass$new() # obtain session data storage$get_sessions() } ## ------------------------------------------------ ## Method `StorageClass$reactive_sessions` ## ------------------------------------------------ ## Only run examples in interactive R sessions if (interactive()) { library(shiny) library(shinystate) storage <- StorageClass$new() server <- function(input, output, session) { refresh_trigger <- reactiveVal(0) session_df <- storage$reactive_sessions( trigger = reactive(list(refresh_trigger(), input$show_load_modal)) ) observeEvent(input$save, { storage$snapshot(session_metadata = list(name = input$save_name)) refresh_trigger(refresh_trigger() + 1) }) } } ## ------------------------------------------------ ## Method `StorageClass$restore` ## ------------------------------------------------ ## Only run examples in interactive R sessions if (interactive()) { library(shinystate) # Create a StorageClass object with default settings storage <- StorageClass$new() # obtain session data session_df <- storage$get_sessions() # restore state # typically run inside a shiny observe or observeEvent call storage$restore(tail(session_df$url, n = 1)) } ## ------------------------------------------------ ## Method `StorageClass$snapshot` ## ------------------------------------------------ ## Only run examples in interactive R sessions if (interactive()) { library(shinystate) # Create a StorageClass object with default settings storage <- StorageClass$new() # save state with timestamp as metadata # typically run inside a shiny observe or observeEvent call storage$snapshot(session_metadata = list(time = Sys.time())) } ## ------------------------------------------------ ## Method `StorageClass$delete` ## ------------------------------------------------ ## Only run examples in interactive R sessions if (interactive()) { library(shinystate) # Create a StorageClass object with default settings storage <- StorageClass$new() # obtain session data session_df <- storage$get_sessions() # delete a session # typically run inside a shiny observe or observeEvent call storage$delete(session_df$url[1]) } ## ------------------------------------------------ ## Method `StorageClass$register_metadata` ## ------------------------------------------------ ## Only run examples in interactive R sessions if (interactive()) { library(shinystate) # Create a StorageClass object with default settings storage <- StorageClass$new() # application server code server <- function(input, output, session) { storage$register_metadata() } }## ------------------------------------------------ ## Method `StorageClass$new` ## ------------------------------------------------ ## Only run examples in interactive R sessions if (interactive()) { # beginning of application library(shiny) library(shinystate) # Create a StorageClass object with default settings storage <- StorageClass$new() # Use a pre-specified directory to store state files # For purposes of this example, use a temporary directory storage <- StorageClass$new(local_storage_dir = tempdir()) # use a custom pins board to store bookmarkable state data # For purposes of this example, use a temporary directory library(pins) board <- board_temp() storage <- StorageClass$new(board_sessions = board) } ## ------------------------------------------------ ## Method `StorageClass$get_sessions` ## ------------------------------------------------ ## Only run examples in interactive R sessions if (interactive()) { library(shiny) library(shinystate) # Create a StorageClass object with default settings storage <- StorageClass$new() # obtain session data storage$get_sessions() } ## ------------------------------------------------ ## Method `StorageClass$reactive_sessions` ## ------------------------------------------------ ## Only run examples in interactive R sessions if (interactive()) { library(shiny) library(shinystate) storage <- StorageClass$new() server <- function(input, output, session) { refresh_trigger <- reactiveVal(0) session_df <- storage$reactive_sessions( trigger = reactive(list(refresh_trigger(), input$show_load_modal)) ) observeEvent(input$save, { storage$snapshot(session_metadata = list(name = input$save_name)) refresh_trigger(refresh_trigger() + 1) }) } } ## ------------------------------------------------ ## Method `StorageClass$restore` ## ------------------------------------------------ ## Only run examples in interactive R sessions if (interactive()) { library(shinystate) # Create a StorageClass object with default settings storage <- StorageClass$new() # obtain session data session_df <- storage$get_sessions() # restore state # typically run inside a shiny observe or observeEvent call storage$restore(tail(session_df$url, n = 1)) } ## ------------------------------------------------ ## Method `StorageClass$snapshot` ## ------------------------------------------------ ## Only run examples in interactive R sessions if (interactive()) { library(shinystate) # Create a StorageClass object with default settings storage <- StorageClass$new() # save state with timestamp as metadata # typically run inside a shiny observe or observeEvent call storage$snapshot(session_metadata = list(time = Sys.time())) } ## ------------------------------------------------ ## Method `StorageClass$delete` ## ------------------------------------------------ ## Only run examples in interactive R sessions if (interactive()) { library(shinystate) # Create a StorageClass object with default settings storage <- StorageClass$new() # obtain session data session_df <- storage$get_sessions() # delete a session # typically run inside a shiny observe or observeEvent call storage$delete(session_df$url[1]) } ## ------------------------------------------------ ## Method `StorageClass$register_metadata` ## ------------------------------------------------ ## Only run examples in interactive R sessions if (interactive()) { library(shinystate) # Create a StorageClass object with default settings storage <- StorageClass$new() # application server code server <- function(input, output, session) { storage$register_metadata() } }
Include shinystate dependencies in your Shiny application UI
use_shinystate()use_shinystate()
## Only run examples in interactive R sessions if (interactive()) { library(shiny) library(shinystate) storage <- StorageClass$new() ui <- function(request) { fluidPage( use_shinystate(), actionButton("bookmark", "Bookmark"), actionButton("restore", "Restore Last Bookmark") ) } }## Only run examples in interactive R sessions if (interactive()) { library(shiny) library(shinystate) storage <- StorageClass$new() ui <- function(request) { fluidPage( use_shinystate(), actionButton("bookmark", "Bookmark"), actionButton("restore", "Restore Last Bookmark") ) } }