Interactive filter for plot

899 views
Skip to first unread message

Jack Simpson

unread,
Sep 21, 2014, 5:53:29 AM9/21/14
to gg...@googlegroups.com
I currently have some data for which I'd love to be able to add an interactive multiple checkbox option that lets the user select which area of the body they want to view data from. At the moment it looks like this:

    areas_data %>%
    ggvis(~Bacilli, ~Actinobacteria) %>%
    filter(area %in% c("Skin", "Oral", "Gut") ) %>%
    layer_points( fill = ~area, size := 50, opacity := 0.5)

I can't figure out how to change the filter line so that it will interactively change the data you're viewing. I've tried inserting input_checkboxgroup into the filter line, but keep receiving an error about how the comparison is only possible for atomic and list types. I've tried a lot of different variants of the line, an example being:

    filter(area %in% input_checkboxgroup(c("Skin" = "Skin", "Oral" = "Oral", "Gut" = "Gut") ) ) %>%

Thanks!

Ian Lyttle

unread,
Sep 21, 2014, 8:45:26 AM9/21/14
to gg...@googlegroups.com
Hi,

I don't have a solution, but maybe I can try to move the ball forward with a reproducible example using the cocaine dataset.

Does this approximate your issue?

library(ggvis)

upper_midwest <- c("Iowa" = IA", "Minnesota" = "MN", "Wisconsin" = "WI")

# works OK
cocaine %>%
  ggvis(~potency, ~price) %>%
  filter(state %in% upper_midwest) %>%
  layer_points(fill = ~state, size := 50, opacity := 0.5)

# error
cocaine %>%  
  ggvis(~potency, ~price) %>%
  filter(state %in% input_checkboxgroup(choices = upper_midwest)) %>%
  layer_points(fill = ~state, size := 50, opacity := 0.5)

Jack Simpson

unread,
Sep 21, 2014, 9:13:15 AM9/21/14
to gg...@googlegroups.com
Hi Ian,

Thanks so much for your help, I was trying different variations of putting the choices as the different strings in my areas column and kept getting the " 'match' requires vector arguments " error. I also tried to force it into different kinds of vectors by trying to convert it with functions like "as.character". Its frustrating because on one hand I feel like I'm so close ,yet at the same time I just can't seem to crack it.

Ian Lyttle

unread,
Sep 21, 2014, 9:21:29 AM9/21/14
to gg...@googlegroups.com
Hi Jack,

I hear you. For me, working with this stuff as it is being developed is an addicting mixture of joy and frustration.

The good news is that Winston and Hadley are pretty good about dealing with the issues, especially if it is made easy for them to see the problem for themselves.

There may be more flexibility in using shiny explicitly - is that an option for you?

Ian 

Ian Lyttle

unread,
Sep 21, 2014, 10:18:45 AM9/21/14
to gg...@googlegroups.com
Here's a shiny version that works for me. 

It's ugly. 

Maybe it can help shake something loose.

library(ggvis)
library(shiny)
library(dplyr)

upper_midwest <- c("Iowa" = "IA", "Minnesota" = "MN", "Wisconsin" = "WI")

shinyApp(
  ui = 
    fluidPage(
      fluidRow(
        column(
          width = 3,
          checkboxGroupInput(
            "state",
            label = "State",
            choices = upper_midwest,
            selected = "IA"
          )
        ),
        column(
          width = 9,
          ggvisOutput("cocaine_state")
        )
      )  
    ),
  
  server = function(input, output, session){

   
    rct_cocaine <- reactive({
      cocaine %>% filter((state %in% input$state))  
    })

    observe(print(input$state))
    observe(glimpse(rct_cocaine()))
    
    # works OK
    rct_cocaine %>%
      ggvis(~potency, ~price) %>%
      layer_points(fill = ~state, size := 50, opacity := 0.5) %>%
      bind_shiny("cocaine_state")
    
  }  
)

Hadley Wickham

unread,
Sep 22, 2014, 8:29:42 AM9/22/14
to Ian Lyttle, gg...@googlegroups.com
Here's a working example:

cocaine %>%
ggvis(~potency, ~price) %>%
filter(state %in% eval(input_checkboxgroup(c("IA", "MN", "WI"),
selected = "WI"))) %>%
layer_points(fill = ~state, size := 50, opacity := 0.5)

I made two changes:

* Both dplyr and ggvis using non-standard evaluation in slightly
different ways, so filter() needs a bit of a hint that
input_checkboxgroup() should be evaluated different. Surround it in
eval() to force it to be evaluated eagerly (this is documented in
filter.ggvis)

* Currently the default selected value is NULL, which selects no
values, and hence ggvis tries to plot an empty data frame and fails
(this is a bug). So you also need to check at least one group by
default. I've filed an issue about this :
https://github.com/rstudio/ggvis/issues/272

Hadley
> --
> You received this message because you are subscribed to the Google Groups
> "ggvis" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggvis+un...@googlegroups.com.
> To post to this group, send email to gg...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
http://had.co.nz/
Reply all
Reply to author
Forward
0 new messages