checkboxGroupInput过滤数据

时间:2018-10-01 21:07:27

标签: r shiny

我无法让我的过滤器盒实际过滤数据。下面是数据和代码的示例。该框显示正常并且可以单击,但是当您选中该框时,它不会过滤数据。

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部分:

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

服务器部分:

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

1 个答案:

答案 0 :(得分:1)

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)