使用dplyr在Shiny中进行动态过滤

时间:2018-10-26 00:22:21

标签: r shiny dplyr

我试图选择让用户选择一列来显示他们想要查看的数据。然后从那里开始,我要根据变量希望查看其数据的变量对它进行过滤。我如何才能对其进行正确过滤?

library(shiny)
library(shinydashboard)
library(tidyverse)

ui <- fluidPage(

    dashboardHeader(title  = "Diamond"),
    dashboardSidebar(disable = TRUE),
    dashboardBody(
        column(2, wellPanel( selectInput("GroupLevel", "Variable",
                                         choices = c("Diamond Cut","Visible Color","Clarity Type"),
                                         selected = "Diamond Cut"),uiOutput("ui"))), 
        dataTableOutput("value")
    )
)

server <- function(input, output) {
    grouping <- list("Diamond Cut" = "cut",
                     "Visible Color" = "color", 
                     "Clarity Type" = "clarity")
    output$ui <- renderUI({
        if (is.null(input$GroupLevel))
            return()
        switch(input$GroupLevel,
               "Diamond Cut" = selectInput("dynamic", "Cut",
                                      choices = sort(unique(diamonds$cut))),
               "Visible Color" = selectInput("dynamic", "Color",
                                      choices = sort(unique(diamonds$color))),
               "Clarity Type" =  selectInput("dynamic", "Clarity",
                                       choices = sort(unique(diamonds$clarity)))
        )
    })

    data_table <-reactive(diamonds %>% 
                              group_by_(grouping[[{input$GroupLevel}]]) %>% 
                              summarise_if(is.numeric,mean) %>% 
                              dplyr::filter(grouping[[{input$GroupLevel}]] == {input$dynamic}))
    output$value <- renderDataTable({data_table()},options = list(searching = FALSE, paging = FALSE))
}

shinyApp(ui = ui, server = server)

0 个答案:

没有答案