Crosstalk with barplots


#1

i am having trouble finding an example of using barplots with cross talk. it seems unresponsive to clicks on the barplots. What am i doing wrong? @cpsievert?

library(crosstalk)
library(plotly)

shared_data <- SharedData$new(data.frame(x=1:10,y=rnorm(10),z=sample(1:4,10,replace = TRUE)),key = ~z)

p1 <- plot_ly(shared_data,x=~x,y=~y,text=~z,type='bar')%>%highlight('plotly_click',color='red')
p2 <- plot_ly(shared_data,x=~x,y=~abs(y),text=~z,type='bar')%>%highlight('plotly_click',color='red')
p3 <- plot_ly(shared_data,x=~x,y=~y,text=~z,type='scatter',mode='markers')%>%highlight('plotly_selected',color='red')


bscols(subplot(p1, subplot(p2,p3,nrows=1),nrows=2))
Session info ------------------------------------------------------------------
 setting  value                       
 version  R version 3.3.3 (2017-03-06)
 system   x86_64, darwin13.4.0        
 ui       RStudio (1.1.383)           
 language (EN)                        
 collate  en_US.UTF-8                 
 tz       America/New_York            
 date     2018-02-04                  

Packages ----------------------------------------------------------------------
 package      * version    date       source                          
 assertthat     0.2.0      2017-04-11 CRAN (R 3.3.2)                  
 base         * 3.3.3      2017-03-07 local                           
 bindr          0.1        2016-11-13 CRAN (R 3.3.2)                  
 bindrcpp       0.2        2017-06-17 CRAN (R 3.3.2)                  
 colorspace     1.3-2      2016-12-14 CRAN (R 3.3.2)                  
 crosstalk    * 1.0.0      2016-12-21 CRAN (R 3.3.2)                  
 curl           3.1        2017-12-12 CRAN (R 3.3.2)                  
 data.table     1.10.4-3   2017-10-27 CRAN (R 3.3.2)                  
 datasets     * 3.3.3      2017-03-07 local                           
 devtools       1.13.4     2017-11-09 CRAN (R 3.3.2)                  
 digest         0.6.13     2017-12-14 CRAN (R 3.3.2)                  
 dplyr          0.7.4      2017-09-28 CRAN (R 3.3.2)                  
 ggplot2      * 2.2.1.9000 2018-02-04 Github (hadley/ggplot2@a2dc248) 
 glue           1.2.0      2017-10-29 CRAN (R 3.3.2)                  
 graphics     * 3.3.3      2017-03-07 local                           
 grDevices    * 3.3.3      2017-03-07 local                           
 grid           3.3.3      2017-03-07 local                           
 gtable         0.2.0      2016-02-26 CRAN (R 3.3.0)                  
 htmltools      0.3.6      2017-04-28 CRAN (R 3.3.2)                  
 htmlwidgets    0.9        2017-07-10 CRAN (R 3.3.2)                  
 httpuv         1.3.5      2017-07-04 CRAN (R 3.3.2)                  
 httr           1.3.1      2017-08-20 CRAN (R 3.3.2)                  
 jsonlite       1.5        2017-06-01 CRAN (R 3.3.2)                  
 labeling       0.3        2014-08-23 CRAN (R 3.3.0)                  
 lazyeval       0.2.1      2017-10-29 CRAN (R 3.3.2)                  
 magrittr       1.5        2014-11-22 CRAN (R 3.3.0)                  
 memoise        1.1.0      2017-04-21 CRAN (R 3.3.2)                  
 methods      * 3.3.3      2017-03-07 local                           
 mime           0.5        2016-07-07 CRAN (R 3.3.0)                  
 munsell        0.4.3      2016-02-13 CRAN (R 3.3.0)                  
 pkgconfig      2.0.1      2017-03-21 CRAN (R 3.3.2)                  
 plotly       * 4.7.1      2017-07-29 CRAN (R 3.3.2)                  
 plyr           1.8.4      2016-06-08 CRAN (R 3.3.0)                  
 purrr          0.2.4      2017-10-18 CRAN (R 3.3.2)                  
 R6             2.2.2      2017-06-17 CRAN (R 3.3.2)                  
 RColorBrewer   1.1-2      2014-12-07 CRAN (R 3.3.0)                  
 Rcpp           0.12.14    2017-11-23 CRAN (R 3.3.2)                  
 rgeolocate     1.0.1      2017-08-02 CRAN (R 3.3.2)                  
 rlang          0.1.6.9003 2018-02-04 Github (tidyverse/rlang@c6747f9)
 scales         0.5.0.9000 2017-11-16 Github (hadley/scales@d767915)  
 shiny          1.0.5      2017-08-23 CRAN (R 3.3.2)                  
 stats        * 3.3.3      2017-03-07 local                           
 tibble         1.3.4      2017-08-22 CRAN (R 3.3.2)                  
 tidyr          0.7.2      2017-10-16 CRAN (R 3.3.2)                  
 tools          3.3.3      2017-03-07 local                           
 utils        * 3.3.3      2017-03-07 local                           
 viridisLite    0.2.0      2017-03-24 CRAN (R 3.3.2)                  
 withr          2.1.1.9000 2018-02-04 Github (jimhester/withr@df18523)
 xtable         1.8-2      2016-02-05 CRAN (R 3.3.0)                  
 yaml           2.1.16     2017-12-12 CRAN (R 3.3.2) 

#2

You can only have one highlight() definition in a single plotly graph (which is why this code chunk returns the Can only have one: highlight warning). Brushing and dragging bars (or points) actually works in this case (because plotly is taking the last highlight definition). This is closer to what you want:

bscols(p1, p2, p3)

PS. somewhat relatedly, plotly has pretty good support for targeting aggregated trace types (e.g., bar, box, violin, etc), but when the selection derives from an aggregated trace type, things can get a bit wonky (this example works because you’ve pre-specified bar heights). This would be a fairly big endeavor to solve completely, and isn’t one of our priorities, but if you’d like to support development, please get in touch.


#3

ok, thanks.

the layout of (1,1,2,3,nrow=2) is important to me. how do i get it to have same layout without nesting subplots?

regarding PS, i’ll put it on my todo list :).


#4

doing what you suggested made my rstudio session freeze with spinning colour wheel


#5
bscols(p1, p2, p3, widths = c(12, 6, 6))

#6

and the more concerning freeze problem?


#7

Try re-running outside of RStudio in a clean session? Works for me inside RStudio…