A first-class output primitive for interactive plotly charts, on par with
PlotOutput and TableOutput. Unlike the {plotly} htmlwidget — which
destroys and recreates the chart on every reactive update — PlotlyOutput
uses Plotly.react() for incremental updates, so a data change preserves
the user's zoom, pan, and selection (via plotly's uirevision).
Usage
PlotlyOutput(
spec,
...,
onClick = NULL,
onHover = NULL,
onUnhover = NULL,
onDoubleclick = NULL,
onDeselect = NULL,
onSelecting = NULL,
onBrushing = NULL,
onLegendClick = NULL,
onLegendDoubleclick = NULL,
onClickAnnotation = NULL,
onSunburstClick = NULL,
onRelayout = NULL,
container = NULL
)Arguments
- spec
A zero-argument function returning a plotly object (from
plotly::plot_ly()orplotly::ggplotly()). Re-evaluated reactively;Plotly.react()diffs the result client-side.- ...
Named state arguments, each a callable (
reactiveVal, store leaf,reactiveProxy) or constant. Recognized:xaxis_range,yaxis_range,xaxis<n>_range,yaxis<n>_range,dragmode,hovermode,selected_ids,trace_visibility.NULLmeans "don't override the spec". Unknown names error — useonRelayoutfor fields outside the table.A
*_rangevalue is a length-2 vector of the axis's own units: numeric for a continuous, log, or category axis; ISO date-time strings (c("2020-04-01", "2020-06-30")) for a date axis, matching how plotly reports and accepts them.selected_idsis the box/lasso selection, keyed on plotly's per-pointids(a character vector of id values). Binding it requires the plot to supplyids(plot_ly(ids = ~key)/aes(ids = key)); because the value is keyed on identity rather than position, the selection survives data changes that renumber points (filtering, sorting, trace recomposition).trace_visibilityis the per-trace on/off state, a sparse named character vector keyed by tracename(c("8" = "legendonly")), values in"true"/"false"/"legendonly". Likeselected_idsit is identity-keyed, so it stays correct across trace recomposition; binding it requires the traces to be named.- onClick, onHover, onUnhover, onDoubleclick
Discrete pointer callbacks. Each receives the (slimmed) plotly event payload.
- onDeselect, onSelecting, onBrushing
Selection-lifecycle notifications.
onDeselectis a side-effect notification only — whenselected_idsis bound, the clear already flows through its prop channel.- onLegendClick, onLegendDoubleclick, onClickAnnotation, onSunburstClick
Discrete interaction callbacks.
- onRelayout
Escape hatch — receives the raw
plotly_relayoutpayload (flat dot-notation keys) for fields outside the translation table.- container
Optional
shiny.tagfor the wrapper element.
Details
PlotlyOutput is a thin wrapper over IridWidget. User-controllable UI
state (axis ranges, drag mode, selection, trace visibility) is exposed as
named reactive arguments, each a two-way prop: bind a reactiveVal,
store leaf, or reactiveProxy and the user's interaction writes back to it.
A reactiveProxy that rejects a write snaps the plot back. Discrete events
(clicks, hovers, legend interactions) are plain on* callbacks.
