Skip to content

as.widget distorts htmlwidget behavior #449

Closed
@ramnathv

Description

@ramnathv

The as.widget function seems to be distorting widget behavior for more recently introduced functionality like onRender. If I remember correctly, the as.widget mechanism was introduced so that piping with dplyr verbs works. While this is convenient, it breaks widget behavior.

For example, if I want to subscribe to the plotly_hover data, I would need to call as.widget first before calling onRender. Simply writing p %>% onRender(...) will not work.

library(plotly)
set.seed(100)
d <- diamonds[sample(nrow(diamonds), 1000), ]
p <- plot_ly(d, x = carat, y = price, text = paste("Clarity: ", clarity),
  mode = "markers", color = carat, size = carat
) 
as.widget(p) %>% 
  onRender("function(el, x){
    el.on('plotly_hover', function(data){
     console.log(data)
    })
  }")

This is a minor inconvenience. The bigger problem comes with renderPlotly on the shiny side. If I want to trigger a shiny input change on hover, I won't be able to do it with renderPlotly, since it applies the as.widget function explicitly to expr. So I cannot pass a widget object to renderPlotly.

My solution to this problem as to write a renderPlotly2 function that removes the as.widget call. I believe this behavior can be incorporated in renderPlotly directly by doing two things:

  1. Make as.widget an S3 method.
  2. as.widget.data.frame is the current as.widget function
  3. as.widget.htmlwidget would be the identity function.

@cpsievert let me know what you think about this. In general, it would be good if the plotly widget is able to expose all the functionality that the base htmlwidgets package provides.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions