简体   繁体   中英

checkboxGroupInput to filter data

I am having trouble getting my filter boxes to actually filter the data. Below is a sample of the data and the code. The boxes show up fine and are clickable, but when you check the box it does not filter the data.

Name   Grade Test1 Test2  
Sam    1st   88    92  
Carla  2nd   82    88  
Matt   1st   90    76  
Kelsey 3rd   92    96




gradelist <- ("1st", "2nd", "3rd")

ui portion:

checkboxGroupInput("grade", "Filter by Grade", gradelist)

server portion:

output$gradetable <- DT::renderDataTable({  
DT::datatable(testscores[input$grade,])
})
library(shiny)
library(dplyr)
library(DT)

ui <- fluidPage(

    fluidRow(

      checkboxGroupInput("grade", "Filter by Grade", c("1st", "2nd", "3rd"), selected = "1st")

    ),
    fluidRow(
      column(12,
             dataTableOutput('gradetable')
      )
    )

)


server <- function(input, output) {

  testscores <- structure(list(Name = structure(c(4L, 1L, 3L, 2L), .Label = c("Carla", 
                                                                              "Kelsey", "Matt", "Sam"), class = "factor"), Grade = structure(c(1L, 
                                                                                                                                               2L, 1L, 3L), .Label = c("1st", "2nd", "3rd"), class = "factor"), 
                               Test1 = c(88L, 82L, 90L, 92L), Test2 = c(92L, 88L, 76L, 96L
                               )), .Names = c("Name", "Grade", "Test1", "Test2"), class = "data.frame", row.names = c(NA, 
                                                                                                                      -4L))

  # product definition
  output$gradetable <- renderDataTable({  

    testscores %>% filter(Grade == input$grade) %>% datatable

  })

}

# Run the application 
shinyApp(ui = ui, server = server)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM