dashR icon indicating copy to clipboard operation
dashR copied to clipboard

Refactor current approach to importing ggplotly and plot_ly objects in DashR

Open rpkyle opened this issue 6 years ago • 2 comments

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::toJSON appears to offer no route to easily implementing hooks as json.dumps does in Python, so we cannot conditionally pre-process toJSON's input on the way to producing its stringified output.

  • "Walking and mutating" A temporary solution is to crawl the layout, recursing into any children of components, to identify properties which may contain x$visdat, and transform their contents using plotly_build prior to passing them along to jsonlite::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_ly and ggplotly objects 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

rpkyle avatar Mar 18 '19 20:03 rpkyle

What is the status of integrating ggplot with dashR?

heoa avatar Aug 14 '19 08:08 heoa

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.

rpkyle avatar Aug 14 '19 12:08 rpkyle