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] |
Maintainer: | Eric Nantz <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.0.0.9001 |
Built: | 2024-11-20 06:06:55 UTC |
Source: | https://github.com/rpodcast/shinystate |
This class provides a set of methods to create and manage Shiny bookmarkable state files.
local_storage_dir
file path to use for storing bookmarkable state files. If not specified, a temporary directory on the host system will be used.
board_sessions
Optional 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_dir
file path to use for storing bookmarkable state files. If not specified, a temporary directory on the host system will be used.
board_sessions
Optional 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
\dontrun{ # beginning of application library(shiny) library(shinystate) # Create a StorageClass object with default settings storage <- StorageClass$new() # Use a local directory called "sessions" to store files storage <- StorageClass$new(local_storage_dir = "sessions") # use a custom pins board to store bookmarkable state data library(pins) board <- board_folder("/path/to/storage_dir") 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()
\dontrun{ # beginning of application library(shiny) library(shinystate) storage <- StorageClass$new() # application UI for displaying session data DT::datatableOutput("session_table") # server logic for displaying session data output$session_table <- DT::renderDT({ storage$get_sessions() }) }
restore()
Restore a previous bookmarkable state session
StorageClass$restore(url, session = shiny::getDefaultReactiveDomain())
url
character with the unique URL assigned to the bookmarkable state session.
session
The Shiny session to associate with the restore operation
\dontrun{ # beginning of application library(shiny) library(shinystate) # restoration of last-saved bookmarkable state file # # beginning of application storage <- StorageClass$new() # application UI to trigger restore shiny::actionButton("restore", "Restore State") # server logic for restoring state observeEvent(input$restore, { session_df <- storage$get_sessions() storage$restore(tail(session_df$url, n = 1)) }) }
snapshot()
Create a snapshot of bookmarkable state
StorageClass$snapshot( session_metadata = NULL, session = shiny::getDefaultReactiveDomain() )
session_metadata
Optional 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
session
The Shiny session to associate with the snapshot operation
\dontrun{ # beginning of application library(shiny) library(shinystate) storage <- StorageClass$new() # application UI to trigger save actionButton("save", "Save State") # server logic for restoring state with timestamp as metadata observeEvent(input$save, { storage$snapshot(session_metadata = list(time = Sys.time())) }) }
delete()
Delete a previous snapshot of bookmarkable state
StorageClass$delete(url)
url
character with the unique URL assigned to the bookmarkable state session.
\dontrun{ # beginning of application library(shiny) library(shinystate) storage <- StorageClass$new() # application UI to let user choose previous session uiOutput("previous_sessions_ui") # application UI to trigger delete shiny::actionButton("delete", "Delete Session") # server logic # populate dynamic UI output$previous_sessions_ui <- renderUI({ session_df <- storage$get_sessions radioButtons( "session_choice", "Choose Session", choices = session_df$url ) }) # perform session deletion observeEvent(input$delete, { req(input$session_choice) storage$delete(input$session_choice) }) }
register_metadata()
Register bookmarkable state storage data collection
This method must be called in the application server function to perform the necessary customizations to bookmark methods
StorageClass$register_metadata()
\dontrun{ # beginning of application library(shiny) library(shinystate) storage <- StorageClass$new() # applicaiton UI code ... # application server code storage$register_metadata() }
clone()
The objects of this class are cloneable with this method.
StorageClass$clone(deep = FALSE)
deep
Whether to make a deep clone.
## ------------------------------------------------ ## Method `StorageClass$new` ## ------------------------------------------------ ## Not run: # beginning of application library(shiny) library(shinystate) # Create a StorageClass object with default settings storage <- StorageClass$new() # Use a local directory called "sessions" to store files storage <- StorageClass$new(local_storage_dir = "sessions") # use a custom pins board to store bookmarkable state data library(pins) board <- board_folder("/path/to/storage_dir") storage <- StorageClass$new(board_sessions = board) ## End(Not run) ## ------------------------------------------------ ## Method `StorageClass$get_sessions` ## ------------------------------------------------ ## Not run: # beginning of application library(shiny) library(shinystate) storage <- StorageClass$new() # application UI for displaying session data DT::datatableOutput("session_table") # server logic for displaying session data output$session_table <- DT::renderDT({ storage$get_sessions() }) ## End(Not run) ## ------------------------------------------------ ## Method `StorageClass$restore` ## ------------------------------------------------ ## Not run: # beginning of application library(shiny) library(shinystate) # restoration of last-saved bookmarkable state file # # beginning of application storage <- StorageClass$new() # application UI to trigger restore shiny::actionButton("restore", "Restore State") # server logic for restoring state observeEvent(input$restore, { session_df <- storage$get_sessions() storage$restore(tail(session_df$url, n = 1)) }) ## End(Not run) ## ------------------------------------------------ ## Method `StorageClass$snapshot` ## ------------------------------------------------ ## Not run: # beginning of application library(shiny) library(shinystate) storage <- StorageClass$new() # application UI to trigger save actionButton("save", "Save State") # server logic for restoring state with timestamp as metadata observeEvent(input$save, { storage$snapshot(session_metadata = list(time = Sys.time())) }) ## End(Not run) ## ------------------------------------------------ ## Method `StorageClass$delete` ## ------------------------------------------------ ## Not run: # beginning of application library(shiny) library(shinystate) storage <- StorageClass$new() # application UI to let user choose previous session uiOutput("previous_sessions_ui") # application UI to trigger delete shiny::actionButton("delete", "Delete Session") # server logic # populate dynamic UI output$previous_sessions_ui <- renderUI({ session_df <- storage$get_sessions radioButtons( "session_choice", "Choose Session", choices = session_df$url ) }) # perform session deletion observeEvent(input$delete, { req(input$session_choice) storage$delete(input$session_choice) }) ## End(Not run) ## ------------------------------------------------ ## Method `StorageClass$register_metadata` ## ------------------------------------------------ ## Not run: # beginning of application library(shiny) library(shinystate) storage <- StorageClass$new() # applicaiton UI code ... # application server code storage$register_metadata() ## End(Not run)
## ------------------------------------------------ ## Method `StorageClass$new` ## ------------------------------------------------ ## Not run: # beginning of application library(shiny) library(shinystate) # Create a StorageClass object with default settings storage <- StorageClass$new() # Use a local directory called "sessions" to store files storage <- StorageClass$new(local_storage_dir = "sessions") # use a custom pins board to store bookmarkable state data library(pins) board <- board_folder("/path/to/storage_dir") storage <- StorageClass$new(board_sessions = board) ## End(Not run) ## ------------------------------------------------ ## Method `StorageClass$get_sessions` ## ------------------------------------------------ ## Not run: # beginning of application library(shiny) library(shinystate) storage <- StorageClass$new() # application UI for displaying session data DT::datatableOutput("session_table") # server logic for displaying session data output$session_table <- DT::renderDT({ storage$get_sessions() }) ## End(Not run) ## ------------------------------------------------ ## Method `StorageClass$restore` ## ------------------------------------------------ ## Not run: # beginning of application library(shiny) library(shinystate) # restoration of last-saved bookmarkable state file # # beginning of application storage <- StorageClass$new() # application UI to trigger restore shiny::actionButton("restore", "Restore State") # server logic for restoring state observeEvent(input$restore, { session_df <- storage$get_sessions() storage$restore(tail(session_df$url, n = 1)) }) ## End(Not run) ## ------------------------------------------------ ## Method `StorageClass$snapshot` ## ------------------------------------------------ ## Not run: # beginning of application library(shiny) library(shinystate) storage <- StorageClass$new() # application UI to trigger save actionButton("save", "Save State") # server logic for restoring state with timestamp as metadata observeEvent(input$save, { storage$snapshot(session_metadata = list(time = Sys.time())) }) ## End(Not run) ## ------------------------------------------------ ## Method `StorageClass$delete` ## ------------------------------------------------ ## Not run: # beginning of application library(shiny) library(shinystate) storage <- StorageClass$new() # application UI to let user choose previous session uiOutput("previous_sessions_ui") # application UI to trigger delete shiny::actionButton("delete", "Delete Session") # server logic # populate dynamic UI output$previous_sessions_ui <- renderUI({ session_df <- storage$get_sessions radioButtons( "session_choice", "Choose Session", choices = session_df$url ) }) # perform session deletion observeEvent(input$delete, { req(input$session_choice) storage$delete(input$session_choice) }) ## End(Not run) ## ------------------------------------------------ ## Method `StorageClass$register_metadata` ## ------------------------------------------------ ## Not run: # beginning of application library(shiny) library(shinystate) storage <- StorageClass$new() # applicaiton UI code ... # application server code storage$register_metadata() ## End(Not run)
Include shinystate dependencies in your Shiny UI
use_shinystate()
use_shinystate()