Skip to content

as.widget distorts htmlwidget behavior #449

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ramnathv opened this issue Feb 2, 2016 · 2 comments
Closed

as.widget distorts htmlwidget behavior #449

ramnathv opened this issue Feb 2, 2016 · 2 comments

Comments

@ramnathv
Copy link

ramnathv commented Feb 2, 2016

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.

@cpsievert
Copy link
Collaborator

Thanks for raising this issue Ramnath. I'm on vacation for a couple weeks, but I'll get working on this right away when I'm back. BTW I'm experimenting with an interface to send plotly events to shiny in #416. I'd love to have your thoughts on it!

@cpsievert
Copy link
Collaborator

@ramnathv your example actually works for me using the dev version of plotly and htmlwidgets (see my session info at the bottom).

You bring up a good point about making renderPlotly() work with an htmlwidget object -- #462 fixes that.

I think the event_data() interface in #416, as opposed to onRender(), will make it much easier to access input events in shiny. That being said, I'm sure those with JS knowledge may want the flexibility of onRender(). It isn't totally clear to me how to access the input data using that approach, though, am I doing it right here? https://gist.github.com/cpsievert/9a899d6c8b83b48ae2aa

Session info -------------------------------------------------------------------
 setting  value
 version  R version 3.2.3 (2015-12-10)
 system   x86_64, darwin13.4.0
 ui       X11
 language (EN)
 collate  en_US.UTF-8
 tz       Asia/Tokyo
 date     2016-02-12

Packages -----------------------------------------------------------------------
 package     * version     date       source
 base64enc     0.1-3       2015-07-28 CRAN (R 3.2.0)
 colorspace    1.2-6       2015-03-11 CRAN (R 3.2.0)
 curl          0.9.5       2016-01-23 CRAN (R 3.2.3)
 devtools    * 1.10.0.9000 2016-01-25 Github (hadley/devtools@2c08890)
 digest        0.6.9       2016-01-08 CRAN (R 3.2.3)
 fortunes      1.5-2       2013-12-14 CRAN (R 3.2.0)
 ggplot2     * 2.0.0.9001  2016-02-09 local
 git2r         0.13.1      2015-12-10 CRAN (R 3.2.2)
 gridExtra     2.0.0       2015-07-14 CRAN (R 3.2.0)
 gtable        0.1.2       2012-12-05 CRAN (R 3.2.0)
 htmltools     0.3         2015-12-29 CRAN (R 3.2.3)
 htmlwidgets * 0.5.2       2016-02-12 Github (ramnathv/htmlwidgets@791cb69)
 httr          1.1.0       2016-01-28 CRAN (R 3.2.3)
 jsonlite      0.9.19      2015-11-28 CRAN (R 3.2.2)
 knitr         1.12.3      2016-01-22 CRAN (R 3.2.3)
 magrittr      1.5         2014-11-22 CRAN (R 3.2.0)
 memoise       1.0.0       2016-01-29 CRAN (R 3.2.3)
 munsell       0.4.2       2013-07-11 CRAN (R 3.2.0)
 plotly      * 2.4.3       2016-02-12 Github (ropensci/plotly@2b9d003)
 plyr          1.8.3       2015-06-12 CRAN (R 3.2.0)
 R6            2.1.2       2016-01-26 CRAN (R 3.2.3)
 Rcpp          0.12.3      2016-01-10 CRAN (R 3.2.3)
 scales        0.3.0.9000  2016-02-10 local
 viridis       0.3.2       2015-12-31 CRAN (R 3.2.3)
 withr         1.0.1       2016-02-04 CRAN (R 3.2.3)
 yaml          2.1.13      2014-06-12 CRAN (R 3.2.0)

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

No branches or pull requests

2 participants