Description
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:
- Make
as.widget
an S3 method. as.widget.data.frame
is the currentas.widget
functionas.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.