dashR
dashR copied to clipboard
Refactor current approach to importing ggplotly and plot_ly objects in DashR
I had a very useful conversation with @alexcjohnson this afternoon, in which we discussed possible solutions to importing figure objects from plotly::plot_ly into DashR.
One pipeline for importing a figure produced by the plotly package looks like (thanks, @nicolaskruchten):
library(plotly)
p <- plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length)
plotly_figure <- plotly_build(p)$x
The resulting plotly_figure object then contains data and layout which should be suitable for dropping into dccGraph. Notably, the output of the plot_ly function includes an x$visdat field.
-
R's JSON formatter lacks hooks The challenge in DashR is that
jsonlite::toJSONappears to offer no route to easily implementing hooks asjson.dumpsdoes in Python, so we cannot conditionally pre-processtoJSON's input on the way to producing its stringified output. -
"Walking and mutating" A temporary solution is to crawl the layout, recursing into any
childrenof components, to identify properties which may containx$visdat, and transform their contents usingplotly_buildprior to passing them along tojsonlite::toJSON.
Mutating the entire object before passing it to jsonlite::toJSON could be problematic if a DashR app is written such that global information about the layout is expected to be in its original, unmutated format. (A possible example involves a Dash app with tabs; @rpkyle will try to mock up a sample to demonstrate.)
- Desired resolution
This issue will be resolved when the approach (currently in development) to supporting
plot_lyandggplotlyobjects is refactored to use a walking and replacing (analogous to what currently happens in Dash for Python) approach in lieu of the walking and mutating strategy above.
@chriddyp
What is the status of integrating ggplot with dashR?
What is the status of integrating ggplot with dashR?
@heoa Dash for R provides full support for inlining Plotly figures, including those produced by the plotly package, with either the plot_ly or ggplotly functions. You also asked this question in https://github.com/plotly/dashR/pull/71, and I've included a working example there.
Dash also allows you to easily include "subplots" produced by the plotly package in your apps.
Have you had a chance to check out our community forum? It's a great resource for general questions like these: https://community.plot.ly.