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()
)Arguments
- values
A
shiny::reactiveValues()object.- fields
Character vector of the field names to track. Use
NULLfor all the fields that exist whenrewindtakes the snapshot.- id
A name for this group.
rewinduses it to keep the registered objects apart, and to label the history entries. The default is the name of thevaluesargument at the call.- session
The Shiny session. The default is the current session.
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.
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
rewindsees 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 <- modelFor an environment, keep a list in the reactive values instead.
Examples
if (interactive()) {
library(shiny)
server <- function(input, output, session) {
rewind_enable()
state <- reactiveValues(selected = character(), zoom = 1)
rewind_track(state, fields = c("selected", "zoom"))
}
}
