Filtering dynamically with checkboxgroupinput in Rshiny

允我心安 提交于 2020-01-25 03:30:14

问题


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?


回答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.



来源:https://stackoverflow.com/questions/35735492/filtering-dynamically-with-checkboxgroupinput-in-rshiny

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!