Search code examples
rggplot2plotlyflexdashboard

crosstalk's filter_select doesn't filter the graph


I'm trying to use the crosstalk package, specifically the filter_select() function, to make an interactive graph with a selection box that filters to speci.

For example, I'd expect the below code (run in e.g. a Flex dashboard) to produce a graph where the countries within the dataset can be filtered by an interactive input.

library(crosstalk)
library(ggplot2)
library(gapminder)
library(plotly)

sd <- SharedData$new(gapminder)
filter_select("country", "Country:", sd, ~ country)

g <- ggplot(sd, aes(year, lifeExp, color = country, group = country)) +
  geom_line()
ggplotly(g)

While I do get a "Country:" selectize input that takes the names of the country, the resulting graph (screenshot below) still includes all countries, not just the ones that I input.

I've tried displaying the graph as a plain ggplot rather than calling ggplotly, and tried running it in a flexdashboard as well as an R notebook, but in no case does the input end up affecting the graph. How can I make the plot interactive?

enter image description here

Session info below. Incidentally, you'll need the dev version of plotly (installed from GitHub) to use plotly with crosstalk.

Session info ----------------------------------------------------------------------------------------------------------------------------------------------
 setting  value                       
 version  R version 3.3.3 (2017-03-06)
 system   x86_64, darwin13.4.0        
 ui       RStudio (1.0.136)           
 language (EN)                        
 collate  en_US.UTF-8                 
 tz       America/New_York            
 date     2017-04-14                  

Packages --------------------------------------------------------------------------------------------------------------------------------------------------
 package       * version    date       source                            
 assertthat      0.2.0      2017-04-11 cran (@0.2.0)                     
 backports       1.0.4      2016-10-24 cran (@1.0.4)                     
 base64enc       0.1-3      2015-07-28 CRAN (R 3.3.0)                    
 colorspace      1.2-6      2015-03-11 CRAN (R 3.3.0)                    
 crosstalk     * 1.0.1      2017-04-07 Github (rstudio/crosstalk@d8f29eb)
 devtools        1.12.0     2016-06-24 CRAN (R 3.3.0)                    
 digest          0.6.12     2017-01-27 cran (@0.6.12)                    
 dplyr           0.5.0.9001 2017-04-11 Github (hadley/dplyr@1b0ae13)     
 evaluate        0.10       2016-10-11 cran (@0.10)                      
 flexdashboard   0.2.0      2016-05-18 CRAN (R 3.3.0)                    
 gapminder     * 0.2.0      2015-12-31 CRAN (R 3.3.0)                    
 ggplot2       * 2.2.1.9000 2017-04-14 Github (hadley/ggplot2@f4398b6)   
 glue            0.0.0.9000 2017-03-30 Github (tidyverse/glue@6e77c3b)   
 gtable          0.2.0      2016-02-26 CRAN (R 3.3.0)                    
 htmltools     * 0.3.5      2016-03-21 CRAN (R 3.3.0)                    
 htmlwidgets     0.8        2016-11-09 cran (@0.8)                       
 httpuv          1.3.3      2015-08-04 CRAN (R 3.3.0)                    
 httr            1.2.1      2016-07-03 cran (@1.2.1)                     
 jsonlite        1.4        2017-04-08 cran (@1.4)                       
 knitr           1.15.19    2017-04-14 Github (yihui/knitr@0c2abf8)      
 labeling        0.3        2014-08-23 CRAN (R 3.3.0)                    
 lazyeval        0.2.0      2016-06-12 cran (@0.2.0)                     
 magrittr        1.5        2014-11-22 CRAN (R 3.3.0)                    
 memoise         1.0.0      2016-01-29 CRAN (R 3.3.0)                    
 mime            0.5        2016-07-07 cran (@0.5)                       
 munsell         0.4.3      2016-02-13 CRAN (R 3.3.0)                    
 plotly        * 4.5.6.9000 2017-04-14 Github (ropensci/plotly@4ffae1c)  
 plyr            1.8.4      2016-06-08 cran (@1.8.4)                     
 purrr           0.2.2      2016-06-18 cran (@0.2.2)                     
 R6              2.2.0      2016-10-05 cran (@2.2.0)                     
 Rcpp            0.12.10    2017-04-13 Github (RcppCore/Rcpp@21d8388)    
 rlang           0.0.0.9006 2017-04-11 Github (hadley/rlang@0756eaf)     
 rmarkdown       1.3        2016-12-21 CRAN (R 3.3.0)                    
 rprojroot       1.1        2016-10-29 cran (@1.1)                       
 rsconnect       0.7        2016-12-21 CRAN (R 3.3.0)                    
 rstudioapi      0.6        2016-06-27 cran (@0.6)                       
 scales          0.4.1.9000 2016-11-28 Github (hadley/scales@89c2a2f)    
 shiny           1.0.1      2017-04-01 cran (@1.0.1)                     
 stringi         1.1.5      2017-04-07 cran (@1.1.5)                     
 stringr         1.2.0      2017-02-18 cran (@1.2.0)                     
 tibble          1.3.0      2017-04-01 cran (@1.3.0)                     
 tidyr           0.6.1      2017-01-10 CRAN (R 3.3.2)                    
 viridisLite     0.2.0      2017-03-24 cran (@0.2.0)                     
 withr           1.0.2      2016-06-20 cran (@1.0.2)                     
 xtable          1.8-2      2016-02-05 CRAN (R 3.3.0)                    
 yaml            2.1.14     2016-11-12 cran (@2.1.14)                    

Solution

  • This was due to a bug in plotly which is now fixed, thanks!

    https://github.com/ropensci/plotly/issues/950