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 plots).
new()
SharedData$new(
data,
key = NULL,
group = createUniqueId(4, prefix = "SharedData")
)
data
A data frame-like object, or a Shiny reactive expression that returns a data frame-like object.
key
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.
If NULL
, then row.names(data)
will be used.
group
The "identity" of the Crosstalk group that widgets will join
when you pass them this SharedData
object. In some cases, you
will want to have multiple independent SharedData
objects link
up to form a single web of widgets that all share selection and
filtering state; in those cases, you'll give those SharedData
objects the same group name. (One example: in Shiny, ui.R and server.R
might each need their own SharedData
instance, even though
they're intended to represent a single group.)
origData()
Return the data frame that was used to create this
SharedData
instance. If a reactive expression, evaluate the
reactive expression. Equivalent to SharedData$data(FALSE, FALSE,
FALSE)
.
SharedData$origData()
groupName()
Returns the value of group
that was used to create
this instance.
SharedData$groupName()
key()
Returns the vector of key values. Filtering is not applied.
SharedData$key()
data()
Return the data (or read and return the data if the data is a Shiny reactive expression).
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.
SharedData$data(withSelection = FALSE, withFilter = TRUE, withKey = FALSE)
withSelection
If `TRUE`, add a selection_
column with
logical values indicating which rows are in the current selection, or
NA
if no selection is currently active.
withFilter
If `TRUE` (the default), only return rows that are part of the current filter settings, if any.
withKey
If `TRUE`, 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).
selection()
Get or set the current selection in the client.
If called without arguments, returns a logical vector of rows that are
currently selected (brushed), or NULL
if 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, sets the selection based on the given value indirectly, by sending the value to the web browser (assumes an active Shiny app or Shiny R Markdown document).
SharedData$selection(value, ownerId = "")
value
If provided, a logical vector of `nrow(origData())` length, indicating which rows are currently selected (brushed).
ownerId
Set this argument to the `outputId` of 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 [shiny::plotOutput()] brush, then `ownerId` should be the `outputId` of that `plotOutput`.
clearSelection()
Clears the selection indirectly, by sending an instruction to the client that it should do so.
SharedData$clearSelection(ownerId = "")
ownerId
See the [SharedData$selection()] method.
clone()
The objects of this class are cloneable with this method.
SharedData$clone(deep = FALSE)
deep
Whether to make a deep clone.