Skip to contents

plotly_widget() and as_widget() turn a figure into an htmlwidget, which is what printing a figure does. Use them to embed figures with htmltools or htmlwidgets::saveWidget().

Usage

plotly_widget(
  fig,
  width = NULL,
  height = NULL,
  elementId = NULL,
  config = NULL,
  bundle = NULL
)

as_widget(x, ...)

Arguments

fig

A go_figure(), a figure from a px_*() function, or a list with data (list of traces), layout, frames and config elements.

width, height

Widget size in CSS units, or NULL for the layout's width/height or to fill the container.

elementId

Optional element id for the widget.

config

Plotly.js configuration options (e.g. list(displayModeBar = FALSE)), merged over those set with config().

bundle

The plotly.js bundle to load: "full", "auto" (the smallest bundle with all the figure's trace types) or the name of a partial bundle (see partial_bundle()). By default, the one set with partial_bundle(), or the plotly.core.bundle option ("full" if unset).

x

A figure or htmlwidget. as_widget() is an S3 generic: the plotly package adds a method for its plot_ly() objects.

...

Passed to plotly_widget().

Value

An object of class htmlwidget.

Details

If the figure's layout has no template, the default template (see default_template()) is applied.

Numeric arrays in traces are sent to the browser as base64-encoded typed arrays ({dtype, bdata} objects, as plotly.py does), which are smaller and faster to load than JSON numbers. JavaScript that reads x.data in htmlwidgets::onRender() sees these objects rather than arrays; read the plotted values from el._fullData instead. plotly_json() shows plain JSON. Vectors of whole numbers are sent as integers; see as_float32() to send other numbers in single precision. Dates and date-times are sent as numbers and turned back into "YYYY-MM-DD HH:MM:SS" strings (in the vector's time zone) in the browser before plotting.

Examples

plotly_widget(list(data = list(list(type = "bar", x = 1:3, y = c(2, 5, 3)))))