Filtering data according to column name and respective column's values in shiny

Viewed 38

I am new in shiny, and maybe it can be easy but I could not make it, so I want to select column name firstly and in second box, it show unique values for selected column, and when choosing any values data table and plot appearing, plot will based on filtered part, thats why it is not hard but my main difficulties to extract interactive filter for data and and in default version, it should be whole data. I share what I have dont it is not working and not correct (this code is without data, I can not share data), I corrected some codes, now I can filter according to one value, but I want to see whole data in default version.

ui <- fluidPage(
  
  sidebarLayout(
    sidebarPanel(
      
      pickerInput("eda_col", "Select variable",
                  choices = c("col 1", "col 2", "col 3", "col 4"), selected = character(0)),
      uiOutput("varselect"),
      # selectInput("xSelector", label = "Select x axis", choices = xAxischoices),
      # selectInput("ySelector", label = "Select the y axis", choices = yAxischoices),
      # selectInput("cyLSelector", label = "Select a cylinder", choices = cylinderChoices),
      
      actionButton("RefreshPlot", label = "Refresh")
    ),
    
    mainPanel(
      dataTableOutput("datatable1")
    )
  )
)



  
  
  
  
  
  
server <- function(input, output) {
    
    
    output$varselect <- renderUI({
      vars <- d[[as.name(input$eda_col)]]
      checkboxGroupInput("level_choice", "Select factors to include", unique(vars))
    })
    
    # vars_r <- reactive({
    #   input$vars
    # })
    # 
    # 
    # res_mod <- callModule(
    #   module = selectizeGroupServer,
    #   id = "my-filters",
    #   data = d,
    #   vars = vars_r
    # )
    # 
    # output$table <- DT::renderDataTable({
    #   req(res_mod())
    #   res_mod()
    # })
    
    
    
    
    filteredData <- reactive({
      
      filteredData <- d %>% filter((!! rlang:: sym(input$eda_col)) == input$level_choice)
      
      return(filteredData)
      
    })
    
    
    output$datatable1 <- renderDataTable({
      
      datatable(filteredData())
    })
  
  }



shinyApp(ui, server)






1 Answers

Please present a full MRE in the future. I have presented your requirements using available dataset gapminder. If this is not your expectation, please update your question using mtcars or gapminder data. Try this

library(gapminder)

choices <- names(gapminder)[1:2]

ui <- fluidPage(
  
  sidebarLayout(
    sidebarPanel(
      
      pickerInput("eda_col", "Select variable",
                  choices = choices, selected = character(0)),
      uiOutput("varselect"),
      # selectInput("xSelector", label = "Select x axis", choices = xAxischoices),
      # selectInput("ySelector", label = "Select the y axis", choices = yAxischoices),
      # selectInput("cyLSelector", label = "Select a cylinder", choices = cylinderChoices),
      
      actionButton("RefreshPlot", label = "Refresh")
    ),
    
    mainPanel(
      dataTableOutput("datatable1")
    )
  )
)

server <- function(input, output) {
  
  
  output$varselect <- renderUI({
    if (is.null(input$eda_col)) vars <- names(gapminder)[1]  ##  define your default variable selection
    else vars <- gapminder[[as.name(input$eda_col)]]
    checkboxGroupInput("level_choice", "Select factors to include", unique(vars))
  })
  
  # vars_r <- reactive({
  #   input$vars
  # })
  # 
  # 
  # res_mod <- callModule(
  #   module = selectizeGroupServer,
  #   id = "my-filters",
  #   data = d,
  #   vars = vars_r
  # )
  # 
  # output$table <- DT::renderDataTable({
  #   req(res_mod())
  #   res_mod()
  # })
  
  
  filteredData <- reactive({
    filteredData <- gapminder %>% filter((!! rlang:: sym(input$eda_col)) %in% input$level_choice)
    return(filteredData)
  })
  
  output$datatable1 <- renderDataTable({
    datatable(filteredData())
  })
  
}
shinyApp(ui, server)
Related