Shiny select input based on other select input

Hello!

I'm working on a school project in Shiny. I am about to make a map with cities/teams/stadium locations. I need some help with select input. I have no idea how to make one selectinput dependent on other eg. By selecting "England" in country input, to have only english teams in team input, and other way if I start from selecting a team from all available teams - I would like to have a selection of related country in select input.

Also is it possible to have a popup option based on the selection but showing the capacity?

and how should I add one more chart under the map that shows the capacity of the chosen stadiums from the biggest to the lowest?

How to make the map and the chart "react" to the slider with the capacity?

I would be very glad for any suggestions

Here is my code:

library(dplyr)
library(leaflet)
library(shiny)

shinyApp(
  ui = fluidPage(
    sliderInput(inputId = "capacity", 
                label = "Stadium Capacity", 
                min = 10000, max = 100000, value = 12000, step = 1000),
    tags$div(title = "This input has a tool tip",
             selectInput(inputId = "country", 
                         label = "Country: ", 
                         choices = sort(unique(data_stadium$Country)))),
    tags$div(title = "This input has a tool tip",
             selectInput(inputId = "team", 
                         label = "Team: ", 
                         choices = sort(unique(data_stadium$Team)))),
    leafletOutput("MapPlot1")
  ),
  
  server = function(input, output) {
    
    output$MapPlot1 <- renderLeaflet({
      leaflet() %>% 
        addProviderTiles("OpenStreetMap.Mapnik") %>% 
        setView(lng = 20, lat = 48, zoom = 4)
    })
    
    observe({
      
      capacity <- input$capacity
      country <- input$country
      team <-input$team
      
      sites <- data_stadium %>% 
        filter(data_stadium$Country %in% country)
      
      leafletProxy("MapPlot1") %>% clearMarkers() %>% 
        addCircleMarkers(lng = sites$Longitude,
                         lat = sites$Latitude,
                         popup = capacity)
                         
                      
    })
  },
  options = list(height = 600)
)

The thing is that if you want select input or any UI thing to be dependent on other UI widgets. You need to create them inside server function with dependencies of other input widget.

You can create them with renderUI function and then show them with UIoutput function in the UI code.

Try it if you still need help search for renderUI in google. I am sure you will understand what to do with it.

Thank you !

I came up with this solution:

library(dplyr)
library(leaflet)
library(shiny)

shinyApp(
  ui = fluidPage(
    sliderInput(inputId = "capacity",
                label = "Stadium Capacity",
                min = 10000, max = 100000, value = 12000, step = 1000),
    tags$div(title = "Select League by Country",
             selectInput(inputId = "country",
                         label = "Country: ",
                         choices = sort(unique(data_stadium$Country)))),
    tags$div(title = "Select Team from Country",
             selectInput(inputId = "team",
                         label = "Team: ",
                         choices = sort(unique(data_stadium$Team)))),
    
    leafletOutput("MapPlot1")
    
  ),
  
  server = function(input, output, session) {
    
    output$MapPlot1 <- renderLeaflet({
  
      leaflet() %>%
        addProviderTiles("OpenStreetMap.Mapnik") %>%
        setView(lng = 20, lat = 48, zoom = 4)
      
    })
    
    observe({
      
      capacity <- input$capacity
      country <- input$country
      team <-input$team
      
      if (is.null(country)) country <- character(0)
      
      # Can also set the label and select items
      
      updateSelectInput(session, "team",
                        label = paste("Team", length(team)),
                        choices = team,
                        selected = tail(team, 1)
                        
      )
      
      sites <- data_stadium %>%
        filter(data_stadium$Country %in% country)
      
      leafletProxy("MapPlot1") %>% clearMarkers() %>%
        addCircleMarkers(lng = sites$Longitude,
                         lat = sites$Latitude,
                         popup = team)
      
    })
  },
  
  options = list(height = 600)
  
)

And it works.

In the source file I have also stadium capacity column. Can I make a slider inupt where I can select the stadiums to show for example only those below 57000 capacity?

You don't have to create them in the server. You can create them in the UI then use an update...Input() function inside an observer in the server to change the contents on the input dynamically based on user-input.

3 Likes

Hello, I wrote an article on this subject. Feel free to check. Filtering a data frame with dependent drop down lists in Shiny thanks Magrittr (aka conditional drop down list)

2 Likes