wizkids121
wizkids121

Reputation: 656

Issue selecting multiple inputs using selectInput

So I put together the foundation of this scrip thanks to the help of an older question I posted on here.

But when I try to add multiple inputs by specifying selectInput(multiple = TRUE...) and then changing all the = on the server side to %in% to account for the multiple choices, I am met with the following error:

Warning: Error in if: argument is of length zero [No stack trace available]

library(shiny)
library(DT)

State <- c("NV", "NV","NV", "MD", "MD", "MD", "MD", "NY", "NY", "NY", "OH", "OH", "OH")
County <- c("CLARK", "WASHOE", "EUREKA", "MONTGOMERY", "HOWARD", "BALTIMORE", "FREDERICK", "BRONX", "QUEENS", "WESTCHESTER", "FRANKLIN", "SUMMIT", "STARK" )
City <- c("Las Vegas", "Reno", "Eureka", "Rockville", "Columbia", "Baltimore", "Thurmont", "Bronx", "Queens", "Yonkers", "Columbus", "Akron", "Canton")
Rating<- c(1,2,3,4,5,6,7,8,9,10,11,12,13)
df <- data.frame(State, County, City, Rating, stringsAsFactors = F)

ui <- fluidPage(
  titlePanel("Test Dashboard "),
  sidebarLayout(
    sidebarPanel(
      selectInput("data1", "Select State", choices = c("All", unique(df$State))),
      selectInput("data2", "Select County", choices = NULL, multiple = TRUE),
      selectInput("data3", "select City", choices = NULL, multiple = TRUE)
    ),
    mainPanel(
      DTOutput("table")
    )
  ))

server <- function(input, output, session){
  
  observeEvent(input$data1, {
    if (input$data1 != "All") {
      updateSelectInput(session, "data2", "Select County", choices = c("All", unique(df$County[df$State %in% input$data1])))
    } else {
      updateSelectInput(session, "data2", "Select County", choices = c("All", unique(df$County)))
    }
  }, priority = 2)
  
  observeEvent(c(input$data1, input$data2), {
    if (input$data2 != "All") {
      updateSelectInput(session, "data3", "Select City", choices = c("All", unique(df$City[df$County %in% input$data2])))
    } else {
      if (input$data1 != "All") {
        updateSelectInput(session, "data3", "Select City", choices = c("All", unique(df$City[df$State %in% input$data1])))
      } else {
        updateSelectInput(session, "data3", "Select City", choices = c("All", unique(df$City)))
      }
    }
  }, priority = 1)
  
  filtered_data <- reactive({
    temp_data <- df
    if (input$data1 != "All") {
      temp_data <- temp_data[temp_data$State %in% input$data1, ]
    }
    if (input$data2 != "All") {
      temp_data <- temp_data[temp_data$County %in% input$data2, ]
    }
    if (input$data3 != "All") {
      temp_data <- temp_data[temp_data$City %in% input$data3, ]
    }
    temp_data
  })
  
  output$table <- renderDT(
    filtered_data()
  )
  
}

shinyApp(ui, server)

Upvotes: 0

Views: 188

Answers (1)

YBS
YBS

Reputation: 21349

You just need req(), in a couple of places. Try this

server <- function(input, output, session){
  
  observeEvent(input$data1, {
    if (input$data1 != "All") {
      updateSelectInput(session, "data2", "Select County", choices = c("All", unique(df$County[df$State %in% input$data1])))
    } else {
      updateSelectInput(session, "data2", "Select County", choices = c("All", unique(df$County)))
    }
  }, priority = 2)
  
  observeEvent(c(input$data1, input$data2), {
    req(input$data2)
    if (input$data2 != "All") {
      updateSelectInput(session, "data3", "Select City", choices = c("All", unique(df$City[df$County %in% input$data2])))
    } else {
      if (input$data1 != "All") {
        updateSelectInput(session, "data3", "Select City", choices = c("All", unique(df$City[df$State %in% input$data1])))
      } else {
        updateSelectInput(session, "data3", "Select City", choices = c("All", unique(df$City)))
      }
    }
  }, priority = 1)
  
  filtered_data <- reactive({
    temp_data <- df
    req(input$data2,input$data3)
    if (input$data1 != "All") {
      temp_data <- temp_data[temp_data$State %in% input$data1, ]
    }
    if (input$data2 != "All") {
      temp_data <- temp_data[temp_data$County %in% input$data2, ]
    }
    if (input$data3 != "All") {
      temp_data <- temp_data[temp_data$City %in% input$data3, ]
    }
    temp_data
  })
  
  output$table <- renderDT(
    filtered_data()
  )
  
}

Upvotes: 2

Related Questions