This is a simple Shiny module for undo/redo history. The Shiny module accepts
an arbitrary reactive data value. Changes in the state of this reactive value
are tracked and added to the user's history. The user can then repeatedly
undo and redo to walk through this stack. The module returns the current
selected value of the reactive from this historical stack, or NULL
when
the app state was changed by the user. Because this reactive can hold
arbitrary data about the state of the Shiny app, it is up to the app
developer to use the returned current value to update the Shiny apps' inputs
and UI elements.
undoHistory(id, value, value_debounce_rate = 500) undoHistoryUI(id, class = NULL, btn_class = "btn btn-default", back_text = NULL, back_title = "Undo", back_icon = "undo", fwd_text = NULL, fwd_title = "Redo", fwd_icon = "redo") undoHistoryUI_debug(id) undoHistoryDemo(display.mode = c("showcase", "normal", "auto"))
id | The module id |
---|---|
value | The reactive expression with the values should be saved for the user's history. This expression can contain arbitrary data and be of any structure as long as it returns a single value (or list). Each change in this value is stored, so the module may not work well for storing large data sets. |
value_debounce_rate | Debounce rate in milliseconds for the |
class | The class applied to the parent button group container that holds the undo/redo buttons. |
btn_class | The classes applied to the buttons. Use a single character vector to apply the same class to both buttons, or a character vector of length 2 to apply individual classes to each button, (undo/redo respectively). |
back_text, fwd_text | The button text |
back_title, fwd_title | The button title (shown on hover) |
back_icon, fwd_icon | The icons used for the buttons, passed to
|
display.mode | The mode in which to display the application. If set to
the value |
The undoHistory()
module returns the currently selected history
item as the user moves through the stack, or NULL
if the last update
was the result of user input. The returned value has the same structure as
the reactive value
passed to undoHistory()
.
undoHistoryUI
: Create the UI elements for the undo/redo buttons
undoHistoryUI_debug
: Debug the saved state. This adds a
shiny::verbatimTextOutput()
UI element that reports the current history
stacks. .$history
contains the historical states that are accessed when
undoing or walking backward and .$future
contains the (psuedo-)future
states to for redo (or walking forward). .$current
contains the current
value that is reported by the undoHistory()
module. Note that this value
will be NULL
when the user is driving the apps state updating.
undoHistoryDemo
: Example app demonstrating usage of the history module.
if (FALSE) { library(shiny) library(shinyThings) ui <- fluidPage( # Add the Undo/Redo buttons to the UI undoHistoryUI("hist", back_text = "Step Backward", fwd_text = "Step Forward"), # A simple text input element whose history we'll track textInput("text", "Enter your text here"), # Debugging elements for the demo verbatimTextOutput("v"), tags$h4("debug"), undoHistoryUI_debug("hist") ) server <- function(input, output, session) { # Use undoHistory() to keep track of the value of input$text undo_app_state <- undoHistory( id = "hist", value = reactive({ # Value must be a reactive, but can be any structure you want req(!is.null(input$text)) input$text }) ) # Use an observer to receive updates from undoHistory() and update the app. observe({ req(!is.null(undo_app_state())) #<< Need to update app whenever not NULL # Manually update app UI and reactive values updateTextInput(session, "text", value = undo_app_state()) }) # Just for debugging output$v <- renderPrint(input$text) } shinyApp(ui, server) }