Pablo Armendariz
Pablo Armendariz

Reputation: 77

Shiny selectInput doesn't react to tab selection

Here is my problem, I have a couple of tabs and I'm trying to update a map according to a choice in the selectInput() function. The select option in inputSelect() is activated and points to Los Angeles which should activate the ObserveEvent() or Observe() function but it doesn't when clicking on the Map tab for the first time. However, I realized that the setView() function doesn't update itself when clicking on the second tab even if I have the selected option set in selectInput(). I want a setView() that reacts to the selected option on the first click on the tab. The selectize option doesn't bring any difference. Here is an example of what I would like to replicate.

library(shiny)
library(leaflet)

ui = bs4DashPage(
  h1('Exemple'),
  br(),
  bs4TabSetPanel(id = 'tabs',
                 side = 'left',
                 bs4TabPanel(tabName = 'First tab',
                             active = TRUE,
                             'Here is some text'),
                 bs4TabPanel(tabName = 'Second tab',
                             active = FALSE,
                             fluidRow(bs4Card(title = 'Inputs',     
                                              solidHeader = TRUE,
                                              width = 2,
                                              closable = FALSE,
                                              selectInput(inputId = 'city',
                                                          label = 'Select a city',
                                                          choices = c('New York','Los Angeles','Seattle'),
                                                          selected = 'Los Angeles',
                                                          selectize = TRUE)),
                                      bs4Card(title = "Map",
                                              width = 10,
                                              leafletOutput('map'))
                                      )))
)


server <- function(input, output, session) {
  output$map <- renderLeaflet({
    leaflet() %>%
      addTiles() %>%
      setView(lng = -95.7129,lat = 37.0902, zoom = 3)
  })
  
  observeEvent(input$city, {
    if(input$city == 'New York'){
      lon <- -74.0060
      lat <- 40.7128
    } else if(input$city == 'Los Angeles'){
      lon <- -118.2437
      lat <- 34.0522
    } else{
      lon <- -122.3321
      lat <- 47.6062
    }
    leafletProxy('map') %>%
      setView(lng = lon, lat = lat, zoom = 5)
  })
}

shinyApp(ui, server)

Upvotes: 0

Views: 200

Answers (0)

Related Questions