An R6 class that represents a shared data frame
...or sufficiently data frame-like object. The primary use for
SharedData is to be passed to Crosstalk-compatible widgets in place
of a data frame. Each
SharedData$new(...) call makes a new "group"
of widgets that link to each other, but not to widgets in other groups.
You can also use a
SharedData object from Shiny code in order to
react to filtering and brushing from non-widget visualizations (like ggplot2
An object of class
R6ClassGenerator of length 25.
SharedData$new(data, key = NULL, group = createUniqueId(4, prefix = "SharedData"))
A data frame-like object, or a Shiny reactive expression that returns a data frame-like object.
Character vector or one-sided formula that indicates the name of the column that represents the key or ID of the data frame. These must be unique, and ideally will be something intrinsic to the data (a proper ID) rather than a transient property like row index.
row.names(data)will be used.
The "identity" of the Crosstalk group that widgets will join when you pass them this
SharedDataobject. In some cases, you will want to have multiple independent
SharedDataobjects link up to form a single web of widgets that all share selection and filtering state; in those cases, you'll give those
SharedDataobjects the same group name. (One example: in Shiny, ui.R and server.R might each need their own
SharedDatainstance, even though they're intended to represent a single group.)
data(withSelection = FALSE, withFilter = TRUE, withKey = FALSE)
Return the data (or read and return the data if the data is a Shiny reactive expression). If
withSelection, add a
selection_column with logical values indicating which rows are in the current selection, or
NAif no selection is currently active. If
withFilter(the default), only return rows that are part of the current filter settings, if any. If
withKey, add a
key_column with the key values of each row (normally not needed since the key is either one of the other columns or else just the row names).
When running in Shiny, calling
data()is a reactive operation that will invalidate if the selection or filter change (assuming that information was requested), or if the original data is a reactive expression that has invalidated.
Return the data frame that was used to create this
SharedDatainstance. If a reactive expression, evaluate the reactive expression. Equivalent to
data(FALSE, FALSE, FALSE).
Returns the value of
groupthat was used to create this instance.
Returns the vector of key values. Filtering is not applied.
selection(value, ownerId = "")
If called without arguments, returns a logical vector of rows that are currently selected (brushed), or
NULLif no selection exists. Intended to be called from a Shiny reactive context, and invalidates whenever the selection changes.
If called with one or two arguments, expects
valueto be a logical vector of
nrow(origData())length, indicating which rows are currently selected (brushed). This value is propagated to the web browser (assumes an active Shiny app or Shiny R Markdown document).
ownerIdargument to the
outputIdof a widget if conceptually that widget "initiated" the selection (prevents that widget from clearing its visual selection box, which is normally cleared when the selection changes). For example, if setting the selection based on a
ownerIdshould be the
clearSelection(ownerId = "")
Clears the selection. For the meaning of
ownerId, see the