1

So I have some data that looks like this:

lat         long            group
40.75482559 -73.84514618    One
40.75759888 -73.84442902    Two
40.75468826 -73.84588623    One
40.82676315 -73.92416382    One
40.82736206 -73.92511749    Two
40.787167   -73.977325      Two
40.82994843 -73.92955017    Two
40.75819016 -73.844841      One
40.82663345 -73.92651367    Two

And I'm trying to dynamically display latlong points by their group.

In my ui.R code, I have:

  checkboxGroupInput("group", 
                     label = h3("Checkbox group"), 
                     choices = list('One' = 1, 
                                    'Two' = 2) 
                     ))

In my server.R code, I have:

  filteredData <- reactive({ 
    df <- randomtaxi[randomtaxi$group == "One",] 
    return(df)
  })

Which works fine. But the moment I try to add something like

    df <- randomtaxi[randomtaxi$group == input$group,] 

It crashes. I've also tried things like:

if (input$team == 1){
    df <- filter(df, group == "One")

and

if (input$team == "One"){
    df <- filter(df, group == "One")

But I can't seem to get it to work. How can I dynamically subset data using CheckboxGroupInput in RShiny?

skathan
  • 641
  • 7
  • 16
  • `input$group` will be a vector? Try using `%in%` - `randomtaxi[randomtaxi$group %in% input$group,] ` – SymbolixAU Mar 01 '16 at 23:25
  • Thanks, tried that, but it still doesn't work -- in fact, it will work if you just hard code a vector (i.e. `groupz = c("One, "Two")` but for some reason it won't take the dynamic content. The error message reads: `Warning: Error in if: missing value where TRUE/FALSE needed` Could it be something with the way I've constructed the checkboxGroupInput UI? – skathan Mar 01 '16 at 23:42
  • inside your `filteredData <- reactive({` put a `print(input$group)` statement and see what it thinks is in there – SymbolixAU Mar 01 '16 at 23:51
  • It says "NULL" -- hmmm, wonder if that's my issue. Any ideas from here? – skathan Mar 01 '16 at 23:54
  • even when you select something? – SymbolixAU Mar 01 '16 at 23:57
  • I take that back -- when I select it gives "1", or "1" "2" – skathan Mar 02 '16 at 00:12
  • Your hunch fixed it once I cleaned up -- feel free to put it as an answer and I'll vote it up – skathan Mar 02 '16 at 00:32
  • Feel free to *tick* the answer too if it answers your question :) – SymbolixAU Mar 02 '16 at 01:20

1 Answers1

1

I think your choices need to be a vector, not a list

checkboxGroupInput("group", 
               label = h3("Checkbox group"), 
               choices = c('One' = 1, 
                           'Two' = 2) 
               ))

Then you need to convert the 1 and 2 to "One" and "Two" respectively so that your filter/subset condition will work.

SymbolixAU
  • 25,502
  • 4
  • 67
  • 139