undoHistory: Undo/Redo History Buttons

Description Usage Arguments Value Functions Examples

View source: R/history.R

Description

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.

Usage

1
2
3
4
5
6
7
8
9
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"))

Arguments

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 value reactive expression. To avoid saving spurious changes in value, the expression is debounced. See shiny::debounce() for more information.

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 shiny::icon(). Set to NULL for no icon. You can also add arbitrary HTML to back_text and fwd_text as the inner HTML of the <button> element.

display.mode

The mode in which to display the application. If set to the value "showcase", shows application code and metadata from a DESCRIPTION file in the application directory alongside the application. If set to "normal", displays the application normally. Defaults to "auto", which displays the application in the mode given in its DESCRIPTION file, if any.

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().

Functions

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
## Not run: 
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)

## End(Not run)

gadenbuie/shinyThings documentation built on Nov. 24, 2019, 6:56 p.m.