Skip to content

Plotly core render - #2503

Draft
msummersgill wants to merge 17 commits into
plotly:masterfrom
msummersgill:plotly-core-render
Draft

msummersgill wants to merge 17 commits into
plotly:masterfrom
msummersgill:plotly-core-render

Conversation

@msummersgill

Copy link
Copy Markdown
Contributor

Note

This is a early stage draft exploring a potential "plotly.core" backend based on msummersgill/plotly.R.core. plotly.core is still in an unstable development phase and has not yet been submitted to CRAN, so this PR is not a merge candidate.

Changes to R/ on plotly-core-render compared with plotly 4.12.1

Comparison of branch plotly-core-render (87fdb204) with upstream plotly 4.12.1 (4ff1c333).

Overall: 21 files changed under R/, +953 / −1,447 lines. Two files are new, four R files plus R/sysdata.rda are removed, and fourteen are modified. A further 91 files under inst/htmlwidgets are removed. Every previously exported function is still exported. Those whose code moved (plotlyOutput(), renderPlotly(), event_data(), plotlyProxy(), partial_bundle(), save_image(), TeX(), highlight(), last_plot(), plotly_json(), schema(), …) now come from plotly.core, imported in R/imports.R. Existing user code keeps the same function names.

What changed, by file

New

  • R/go_figure.R (+415), the heart of the change. plot_ly() and ggplotly() objects are still built by plotly_build(). as_go_figure(), now exported, converts the result to a plotly.core figure, adding each trace with plotly.core's add_<type>() and the layout with update_layout(). That validates everything against the bundled plotly.js 4.3 schema, and invalid attributes are dropped with a warning. The file also contains:
    • the rendering hooks: render_go_figure(), as_widget.plotly(), and print, knit and as.tags methods;
    • partial_bundle.plotly();
    • plotly_build.go_figure(), so plotly.core figures can go into subplot();
    • generated add_<type>.plotly() methods for every trace type plotly doesn't define itself;
    • moving crosstalk keys and sets out of the traces into widget settings (extract_keys());
    • upgrades for features plotly.js has removed: Mapbox traces and layout become MapLibre map, heatmapgl becomes heatmap, legacy polar becomes scatterpolar, and title aliases are rewritten.
  • R/transforms.R (+220): plotly.js 2 removed trace transforms (filter, groupby, sort, aggregate), so they are now applied in R before rendering. Plots using them keep working.

Removed (moved to plotly.core)

  • R/shiny.R (−274) and R/proxy.R (−112): Shiny bindings, event registration and plotlyProxy().
  • R/partial_bundles.R (−216): bundle selection. plotly.core ships the bundles, so plotly 4.x's download from cdn.plot.ly is gone.
  • R/last_plot.R (−21).
  • R/sysdata.rda: the stored plotly.js schema. It's now read lazily from plotly.core (R/onLoad.R, plotly_schema()).
  • inst/htmlwidgets/ (91 files): the widget JavaScript, plotly.js and its locales, MathJax 2, selectize, colourpicker and the typedarray/setPrototypeOf polyfills.

Largely emptied

  • R/kaleido.R (−270): save_image() uses plotly.core's Chrome-based export; kaleido() remains as a deprecated wrapper.
  • R/mathjax.R (−64): TeX() and the MathJax dependencies now come from plotly.core; a small local-MathJax helper stays for orca().
  • R/highlight.R (−222): highlight() and attrs_selected() are re-exported from plotly.core. Only highlight_defaults() remains, still used by plotly_build().
  • R/dev.R (−26): plotly_json() now comes from plotly.core.

Modified

  • R/plotly.R:
    • as_widget() becomes an S3 generic (method as_widget.list) creating the plotly_core widget;
    • the polyfill and dependency plumbing is gone;
    • plot_mapbox() no longer sets a Mapbox token (MapLibre maps don't need one).
  • R/plotly_build.R:
    • calls the transform and legacy-polar upgrades, and upgrade_removed();
    • drops verify_attr_names(), the Shiny event defaults and the partial-bundle check, all now handled by plotly.core;
    • replaces tibble/dplyr/purrr calls in the busiest code with base R helpers (columns_df(), data_groups(), transpose_list());
    • avoids slow unique() on large matrices (is_singular()) and needless list copies.
  • R/utils.R: those base R helpers, a base order() in place of dplyr::arrange(), digest in place of rlang::hash, and removal of the old schema check.
  • R/add.R: the docs now point to plotly.core's generated documentation of every trace attribute.
  • R/layout.R:
    • config() becomes a method, config.plotly();
    • locales come from plotly.core;
    • rangeslider() only sets a range when both ends are given.
  • R/subplots.R: mapbox becomes map, and plotly.core figures are accepted as input.
  • R/ggplotly.R: two attribute fixes found by the new validation: yanchor = "center" becomes "middle", and linetype becomes dash.
  • R/sf.R: a base R join replaces dplyr::left_join.
  • R/imports.R: imports plotly.core (except schema) and re-exports its functions. ggplot2 is imported without last_plot and set_last_plot.
  • DESCRIPTION:
    • requires R ≥ 4.1 and imports plotly.core (from GitHub via Remotes);
    • promises is dropped from Imports;
    • reticulate is replaced by chromote in Suggests.

Why things moved to plotly.core

  1. One renderer for every way of making a plot. Rendering a figure doesn't depend on whether it came from plot_ly(), ggplotly(), go_figure() or px_*(). That covers the widget JavaScript, plotly.js, Shiny events and proxies, partial bundles, static export, MathJax and crosstalk highlighting. Keeping them in the shared lower layer means one widget and one copy of plotly.js, and fixes like typed arrays, crosstalk and proxy streaming reach all four interfaces at once. Two widgets would drift apart.
  2. Validation from the schema, not by hand. plotly.core's validators and documentation are generated from plotly.js's own schema. They replace plotly's hand-maintained verify_attr_names() and stored schema, and catch real bugs: the two ggplotly() fixes above came from this.
  3. Current plotly.js. plotly.core bundles plotly.js 4.3, and plotly 4.x was written for older releases. go_figure.R and transforms.R translate features plotly.js has since removed (Mapbox, heatmapgl, legacy polar, transforms, title aliases), so existing plots keep working on the new renderer.
  4. No Python for static export. save_image() uses plotly.core's Chrome-based export, which sets itself up from R, in place of Kaleido through reticulate. kaleido() stays as a deprecated wrapper.
  5. Speed. Typed-array serialisation in plotly.core, plus base R in the busiest parts of plotly_build(), account for the gains measured in the render-performance vignette.
  6. One copy of the assets. plotly no longer ships its own plotly.js, locales and MathJax: it relies on plotly.core's instead of keeping a second, out-of-date set.

What stays in plotly is the R-specific layer: plot_ly() and its data-mapping grammar, ggplotly(), subplot(), layout(), style(), highlight_key(), animation helpers and plotly_build(). plotly.core is plotly.py's equivalent, and plotly is the R grammar on top of it.

Matthew Summersgill added 17 commits November 13, 2026 14:10
plot_ly() objects are still built by plotly_build(); the result is then
converted to a plotly.core figure (as_go_figure()), adding each trace with
plotly.core's add_<type>() and the layout with update_layout(), and
rendered with plotly.core's widget, which bundles plotly.js 4.

- add_trace() and the add_*() functions plotly.core also has (add_pie(),
  add_histogram(), ...) are methods of plotly.core's generics; plotly.core's
  add_<type>() functions are re-exported and add lazy layers to plot_ly()
  objects.
- Trace helpers without a plotly.js trace type (add_markers(), add_lines(),
  add_bars(), add_boxplot(), ...) are deprecated (once per session) and keep
  working as before.
- Features plotly.js has removed are upgraded during the build: Mapbox
  traces/layout to MapLibre map ones, heatmapgl, legacy polar charts, title
  aliases, and trace transforms (applied in R). The build checks attributes
  against plotly.core's schema.
- Shiny functions, save_image()/kaleido(), plotly_json(), last_plot() and
  TeX() come from plotly.core; orca(), export(), partial_bundle() and the
  Chart Studio functions are deprecated or defunct shims.
- Removes the bundled plotly.js 2 (inst/htmlwidgets) and the code that only
  it and Chart Studio used.
- Removes verify_attr_names() and attrs_name_check(): plotly.core validates
  trace and layout attributes (names, nested attributes and values) when a
  plot is converted, so unknown attributes were reported twice. Unknown
  config() options, which only that check covered, are now dropped with a
  warning when the plot is rendered.
- Removes the Chart Studio `apiSrc` tagging and the I() boxing of one-value
  data arrays from verify_attr(); plotly.core sends data arrays as arrays.
  Tests that checked the AsIs class now check the JSON plotly.js receives.
Removes R/shiny.R (shiny_defaults_set() and shiny_event_defaults()):
plotly.core's widget, event_register() and event_unregister() already
apply the same default events, so plotly_build() no longer fills them in.
The test of the defaults checks the rendered widget.
partial_bundle() is re-exported from plotly.core, which ships plotly.js's
partial bundles and picks the smallest one with the plot's trace types
(it was a deprecated no-op); the bundle choice is carried over when a plot
is converted to a plotly.core figure.
htmlwidgets collects a widget's dependencies before its preRenderHook runs,
so those of the plotly.core widget a plot_ly() object is converted to in
the hook were lost (plots rendered blank once plotly.core stopped listing
plotly.js in its widget YAML; locales, MathJax and linked-highlighting
libraries were already missing).

- print(), knit_print() and htmltools::as.tags() of plot_ly() objects
  render plotly.core's widget (as_widget()), with all its dependencies.
- For htmlwidgets::saveWidget(): plot_ly() widgets get plotly.js and
  crosstalk when created, config() adds locale and MathJax dependencies and
  highlight() selectize and colourpicker, as in plotly 4.x.
- partial_bundle() for plot_ly() objects builds the plot and replaces its
  plotly.js dependency.
- traceify() computes each level's rows once (not once per column) and
  doesn't copy data when a level covers every row.
- arrange_safe() sorts with radix order() and only reorders rows that
  aren't already in order; incomplete rows are removed only if there are
  any; the group index is only computed for traces drawn by group.
- The build no longer calls vctrs, dplyr, tibble, rlang, tidyr or purrr:
  base helpers replace as_tibble(), arrange(), group_vars(), transpose(),
  eval_tidy(), is_na()/is_atomic() and (in fortify_sf()) left_join();
  digest::digest() replaces rlang::hash() for selectize ids; the unused
  group_by() of segment data is dropped.

About 2x faster on large plots (e.g. 0.70 s -> 0.30 s for three
1M-point WebGL traces); the JSON sent to plotly.js is unchanged.
… deprecated

- plotly_build.go_figure() so subplot() can combine go_figure()s with
  plot_ly()/ggplotly() objects; no frames warning for empty frames
- Drop the once-per-session deprecation of add_markers(), add_lines(), etc.
- add_trace docs point to the plotly.core functions documenting every
  supported attribute
element_blank() text gave layout fonts size 0, which plotly.core's
validators warn about (e.g. tickfont.size). Found by regression testing
the plotly_book examples.
is_singular() gives the same result as length(uniq(x)) == 1 without
unique.matrix(), which took 0.2 s for a 2000x2000 z.
Their deprecation/removal isn't part of the plotly.core backend and can
be a separate change. Only what the backend needs is adapted:
plotlyMainBundlePath() points to plotly.core's plotly.js and
mathjax_path() is kept for orca().
plotly.core's save_image() now uses headless Chrome instead of the kaleido
Python package. kaleido() is kept as a deprecated wrapper around
plotly.core::chrome_exporter(). Visual tests use the Chrome exporter;
reticulate is replaced by chromote in Suggests and the CI steps installing
Python and kaleido are removed.
plotly.js widens one axis range to keep a map's aspect ratio, so ggplot2's
graticule, which covers only the panel's ranges, stopped short of the plot
area (more visibly with plotly.js 4, which no longer widens the margin for
the last tick label). The graticule is now computed over a wider area with
the same breaks and crs, and clipped to the plot area by plotly.js.
plotly.core's highlight() takes the same options (and CSS colours from
toRGB()); plotly_build() still completes them in supply_highlight_attrs().
pak takes a remote's package name from the GitHub repository name
(plotly.R.core), so the plotly.core import wasn't resolved in CI.
plot_mapbox() traces are upgraded to scattermap, and the bounds to fit
become the widget's fitBounds setting (p$x$fitBounds$map). MapLibre needs
no Mapbox token, so these two tests no longer skip without one.
@msummersgill

Copy link
Copy Markdown
Contributor Author

Visual snapshot difference summary attached for the 148 Visual test failures. Plot margins behavior for geo plots is slightly different in plotly.js latest and represents the closest thing to a true behavior change.

Visual snapshot review (run 44212108035).html

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant