Dynamic Filtering with dplyr in Shiny

dplyr
shiny
#1

I am trying to select have the user select a column to show data that they would like to see. Then from there I am filtering it by which factor from the variable they would like to see the data for. How can I get it to filter correctly?

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

shinyApp(ui = ui, server = server)
0 Likes