Christofray
Christofray

Reputation: 1

Need help making dependent dropdown boxes in the RStudio package Shiny

I have two datasets, one with a list of two hundred cities and their corresponding state and another much larger dataset that I'd like to make an app to sort through. I need help making two drop down boxes in my shiny app where the first is the state variable and the second is the list of cities within that chosen state. I then want those selections to filter the much larger, second dataset in the output. I've tried solutions from several similar but slightly different examples online, but I'm having trouble translating it to what I'm doing.

So far I have this:

ui <- fluidPage(
    headerPanel(''),
    sidebarPanel(
            #add selectinput boxs
      htmlOutput("state_selector"),
      htmlOutput("city_selector"),
    ),
    mainPanel(
        fluidRow(
        # Create a new row for the table.
        DT::dataTableOutput("table")
    )

 server <- function(session, input, output) {
    
    output$state_selector = renderUI({
      selectInput("state", label = h4("State"), 
                  choices = as.character(unique(citystatedata$state)), selected = NULL)
    })
    
    output$city_selector = renderUI({
      
      data_available = citystatedata[citystatedata$State == input$state, "state"]
    
      selectInput(inputId = "city", #name of input
                  label = "City", #label displayed in ui
                  choices = unique(data_available), #calls list of available cities
                  selected = unique(data_available)[1])
    })

shinyApp(ui = ui, server = server)

I tried to take out the portions of the code that weren't specifically related to the drop down boxes, since that's what I was more specifically asking about. So I'm sorry if I've left anything out! Let me know if I need to include anything else

Upvotes: 0

Views: 501

Answers (1)

YBS
YBS

Reputation: 21287

Using available gapminder data, you can try this.

df <- gapminder
df$state <- gapminder$continent
df$city <- gapminder$country
citystatedata <- df

ui <- fluidPage(
  headerPanel('Test'),
  sidebarPanel(
    #add selectinput boxs
    uiOutput("state_selector"),
    uiOutput("city_selector"),
  ),
  mainPanel(
    fluidRow(
      # Create a new row for the table.
      DTOutput("table")
    )
  )
)

server <- function(session, input, output) {
  
  output$state_selector = renderUI({
    selectInput("state", label = h4("State"), 
                choices = as.character(unique(citystatedata$state)), selected = NULL)
  })
  
  output$city_selector = renderUI({
    
    data_available = citystatedata[citystatedata$state == req(input$state),]
    
    selectInput(inputId = "city", #name of input
                label = "City", #label displayed in ui
                choices = unique(data_available$city), #calls list of available cities
                selected = 1)
  })
  
  mydt <- reactive({
    citystatedata %>% filter(citystatedata$state == req(input$state) &  citystatedata$city %in% req(input$city))
  })
  
  output$table <- renderDT(mydt())
  
}

shinyApp(ui = ui, server = server)

Upvotes: 0

Related Questions