我想让用户选择是否在Leaflet地图上绘制某些标记。这取决于输入$ competitorchoice,它可以是TRUE或FALSE。我希望当此值为TRUE时,某些标记仅绘制。我会在if中使用if子句和addMarkers,但这不起作用......
示例代码如下所示:
output$mymap<-renderLeaflet({
leaflet(data=get(paste(input$type,".locations",sep = ""))[[input$stations]]) %>%
addMarkers(~lon, ~lat,data=terminals,icon=termi,popup = ~name_terminal))
此后,我想添加一个条件addMarkers.It仅在输入$ competitorchoice为TRUE时被调用...
答案 0 :(得分:1)
由于您没有提供可重现的示例,因此我基于Leaflet教程数据集。一种方法是使具有反应性的复选框输入。这是我的尝试,可以通过单击框来启用/禁用标记。
library(shiny)
library(leaflet)
ui <- fluidPage(
leafletOutput("mymap"),
p(),
# Add checkboxInput() to turn markers on and off:
checkboxInput("markers", "Turn On Markers", FALSE)
)
server <- function(input, output, session) {
# Some random data:
dat <- data.frame(long = rnorm(40) * 2 + 13, lat = rnorm(40) + 48)
# observe() looks for changes in input$markers and adds/removes
# markers as necessary:
observe({
proxy <- leafletProxy("mymap", data = dat)
proxy %>% clearMarkers()
if (input$markers) {
proxy %>% addMarkers()
}
})
# Render basic map with any element that will not change.
# Note: you can change the starting zoom/positioning/et cetera
# as appropriate:
output$mymap <- renderLeaflet({
leaflet(dat) %>% addProviderTiles("Stamen.TonerLite", options = providerTileOptions(noWrap = TRUE))
})
}
shinyApp(ui, server)