这是我的问题,我有几个选项卡,我正在尝试根据selectInput()
函数中的选择更新地图。select
选项 ininputSelect()
已激活,并指向Los Angeles
应激活ObserveEvent()
or功能的选项,但在第一次Observe()
单击选项卡时不会。Map
但是,我意识到setView()
即使我selected
在selectInput()
. 我想要一个对第一次单击选项卡时的选项setView()
做出反应selected
的选项。该selectize
选项不会带来任何差异。这是我想要复制的示例。
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)
谢谢您的帮助。