crosstalk icon indicating copy to clipboard operation
crosstalk copied to clipboard

Two keys to filter on with crosstalk

Open wolski opened this issue 7 years ago • 1 comments

I am wondering if it is possible to filter the data on a second attribute/key when using crosstalk::SharedData?

I would like to modify the example:

https://stackoverflow.com/questions/48581598/filter-two-tables-with-crosstalk/49327147#49327147

to introduce an additional variable e.g. sex, and to have an additional select_filter, which will enable me to subset the data by sex (male, female) first, before in addition selecting by owner.

wolski avatar Sep 25 '18 11:09 wolski

If you're just doing filtering you can do something like the following:

library(plotly)
library(crosstalk)

df <- data.frame(x = rnorm(100),
               y = rnorm(100),
               class = factor(rep(1:5, each = 20)),
               peeps = rep(c(
                 "bob","joe","jane"
               ),length.out = 100),
               grouping_first = factor(rep(1:2, each = 50)),
               grouping_second = factor(rep(1:2, length.out = 50)))

theme_set(theme_minimal())

sd <- SharedData$new(df, group = "individual")

c1 <- filter_checkbox(id = "group1",sharedData = sd,group = ~grouping_first,
              label = "random dimension 1")
c2 <- filter_checkbox(id = "group2",sharedData = sd,group = ~grouping_second,
              label = "random dimension 2")

g1 <- sd %>% ggplot(aes(x =x ,y =y, color = class, text = peeps)) + 
geom_point() + labs(title = "Everyone")

i1 <- ggplotly(g1, tooltip = "text") %>%
highlight(on = "plotly_click",off = "plotly_doubleclick") 

bscols(widths = c(3,9),
     list(c1,c2), i1)

@cpsievert et al. related to this problem, it seems like there is an issue between having SharedData$new include a specified key. For example. just updating the above code to group by peep using

sd <- SharedData$new(df,key = ~peeps,
                     group = "individual")

makes the filtering fail (but the key=~peeps grouping works).

@kaylafrisoli

benjaminleroy avatar Mar 06 '19 17:03 benjaminleroy