Show different results on datatable in Shiny

My question is how to save if else statement on Server.R side to show datatable appropriately based on different filters.

For example, currently, I have a checkboxgroup to select State, a checkboxgroup to select MSA, a checkboxgroup to select Property Type, and 7 sliders to select values for different variables. I want to show the datatabele while all filters selected, the datatable when a single filter selected, and the datatable when partially selected.

Currently, I made it work by writing a couple if else statement. I believe it's a stupid way but I am not sure what is good way to use reactive.

Thanks.

Hi, welcome!

It's hard to help you with out seen any code, so please try to turn this into a reproducible example of the problematic code inside your shiny app. You can follow this guide to do it.

Thanks.

Here is my code:

shinyApp(
  ui = ffluidPage(
        sidebarLayout(
          sidebarPanel(
            checkboxInput("quartercheckBox_portfolio", "Quarter", value = TRUE),
            conditionalPanel(
              condition = "input.quartercheckBox_portfolio == 1",
              uiOutput("quarterOutput_portfolio")
            ),
            checkboxInput("property_typecheckBox_portfolio", "Property Type", value = TRUE),
            conditionalPanel(
              condition = "input.property_typecheckBox_portfolio == 1",
              uiOutput("property_typeOutput_portfolio")
            ),
            checkboxInput("statecheckBox_portfolio", "State", value = FALSE),
            conditionalPanel(
              condition = "input.statecheckBox_portfolio == 1",
              uiOutput("stateOutput_portfolio")
            ),
            checkboxInput("msacheckBox_portfolio", "MSA", value = FALSE),
            conditionalPanel(
              condition = "input.msacheckBox_portfolio == 1",
              uiOutput("msaOutput_portfolio")
            ),
            uiOutput("size_sf_units_keysOutput_portfolio"),
            uiOutput("acquisition_dateOutput_portfolio"),
            uiOutput("occupancyOutput_portfolio")
            width = 3
          ),
          mainPanel(dataTableOutput("portfolio"))
        ),
  server = function(input, output, session) {
    output$plot <- renderDataTable({
    whole <- cf_portfolio %>%
        filter(          
          size_sf_units_keys >= minsize_sf_units_keys,
          size_sf_units_keys <= maxsize_sf_units_keys,
          acquisition_date >= minacquisition_date,
          acquisition_date <= maxacquisition_date,
          occupancy >= minoccupancy,
          occupancy <= maxoccupancy)

    if (input$msacheckBox_portfolio == FALSE & input$statecheckBox_portfolio == FALSE) {
      whole %>%
        datatable(extensions = c("Buttons"))
    } else if (input$msacheckBox_portfolio == TRUE & input$statecheckBox_portfolio == FALSE) {
      whole %>%
        filter(msa %in% input$msaInput_portfolio) %>%
        datatable(extensions = c("Buttons"))
    } else if (input$msacheckBox_portfolio == FALSE & input$statecheckBox_portfolio == TRUE) {
      whole %>%
        filter(state %in% input$stateInput_portfolio) %>%
        datatable(extensions = c("Buttons"))
    } else if (input$msacheckBox_portfolio == TRUE & input$statecheckBox_portfolio == TRUE) {
      whole %>%
        filter(state %in% input$stateInput_portfolio) %>%
        filter(msa %in% input$msaInput_portfolio) %>%
        datatable(extensions = c("Buttons"))
    }
    })
  }
)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.