Learn R Programming

rewind (version 0.4.0)

rewind_track: Include server-side reactive values in the history

Description

rewind captures inputs automatically. It cannot see the state that you keep in a shiny::reactiveValues() object. Register that object here. rewind then records the registered fields with the inputs, and writes them back at an undo.

Usage

rewind_track(
  values,
  fields = NULL,
  id = NULL,
  session = shiny::getDefaultReactiveDomain()
)

Value

TRUE, invisibly.

Arguments

values

A shiny::reactiveValues() object.

fields

Character vector of the field names to track. Use NULL for all the fields that exist when rewind takes the snapshot.

id

A name for this group. rewind uses it to keep the registered objects apart, and to label the history entries. The default is the name of the values argument at the call.

session

The Shiny session. The default is the current session.

Environments and R6 objects

rewind keeps each value as it is, and does not copy it. For a vector, a list or a data frame, that is a true record, because R copies such a value when you change it.

An environment and an R6 object are different. The history holds the object itself, not a copy. A change inside the object thus has two effects:

  • It is not a step. When you assign the same object back, Shiny sees no change, so rewind sees none.

  • An undo cannot bring back the old contents. Each entry holds the same object, as it is now.

To make each change a step, change a copy, then assign the copy:

model <- state$model$clone(deep = TRUE)
model$fit(data)
state$model <- model

For an environment, keep a list in the reactive values instead.

Details

Register only the values that are true state. A derived value or a cached value does no harm, but it has no use. Do not register a value that an observer computes again immediately. The undo step then appears to do nothing.

Examples

Run this code
if (interactive()) {
  library(shiny)

  server <- function(input, output, session) {
    rewind_enable()
    state <- reactiveValues(selected = character(), zoom = 1)
    rewind_track(state, fields = c("selected", "zoom"))
  }
}

Run the code above in your browser using DataLab