使用 ShinyWidget pickerInput 取消选择 Leaflet 中的图层

问题描述 投票:0回答:1

我正在使用 ShinyWidget 的 pickerInput 功能来允许用户选择要在 Leaflet 中显示的多个图层(空间数据)。选中多个框会根据需要显示图层,但是,在取消选中输入菜单中的框后,我无法隐藏/取消选择图层。

我的app.R脚本中的关键代码:

tn_data <- c("Rail"="rail1", "Airports"="airports1", "Ferries"="ferries1")

pickerInput(inputId = "pickv", label = "Transportation", choices = tn_data, multiple = TRUE),

rail_vn <- readOGR(dsn = "./geospatial_files/osm", layer = "gis.osm_railways_free_1")

server <- function(input, output, session) {

observeEvent(input$pickv, {
  if (input$pickv == "rail1"){  # this diplays just the rail layer
  proxy <- leafletProxy("map")
  proxy %>% addPolylines(data=rail_vn, weight = 2, group = "railv", color = "#7f0000")}
  else {proxy %>% clearGroup("railv")} # this does not work, unable to deselect/hide layer in Leaeflet
 }
)

以前,当我使用 checkboxInput 时,我可以使用clearGroup 函数从 Leaflet 中取消选择图层,但这在使用 pickerInput 时不起作用。

我还没有找到任何类似的例子,其中pickerInput与Leaflet一起使用。

r shiny r-leaflet
1个回答
0
投票

我相信你应该在 if then 之外定义代理,这样在 else 上也可用。

observeEvent(input$pickv, {
  proxy <- leafletProxy("map")
  if (input$pickv == "rail1"){  # this diplays just the rail layer
     proxy %>% addPolylines(data=rail_vn, weight = 2, group = "railv", color = "#7f0000")}
  else {
        proxy %>% clearGroup("railv")} # this does not work, unable to deselect/hide layer in Leaeflet
 }
© www.soinside.com 2019 - 2024. All rights reserved.