Skip to contents

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() or plotly::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. NULL means "don't override the spec". Unknown names error — use onRelayout for fields outside the table.

A *_range value 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_ids is the box/lasso selection, keyed on plotly's per-point ids (a character vector of id values). Binding it requires the plot to supply ids (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_visibility is the per-trace on/off state, a sparse named character vector keyed by trace name (c("8" = "legendonly")), values in "true" / "false" / "legendonly". Like selected_ids it 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. onDeselect is a side-effect notification only — when selected_ids is bound, the clear already flows through its prop channel.

onLegendClick, onLegendDoubleclick, onClickAnnotation, onSunburstClick

Discrete interaction callbacks.

onRelayout

Escape hatch — receives the raw plotly_relayout payload (flat dot-notation keys) for fields outside the translation table.

container

Optional shiny.tag for the wrapper element.

Value

An irid_widget construct.

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.