【问题标题】:R Shiny: Deselecting layers in Leaflet with ShinyWidget pickerInputR Shiny:使用 ShinyWidget pickerInput 在 Leaflet 中取消选择图层
【发布时间】:2018-06-20 02:19:53
【问题描述】:

我正在使用 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 leaflet


    【解决方案1】:

    我认为您应该在 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
     }
    

    【讨论】:

    • 感谢您的建议,但我仍然无法取消选择图层。我也尝试过使用代理 %>% clearShapes()
    • 我注意到,如果我取消选中显示图层 (rail1) 的框,然后选择另一个图层,则 rail1 图层将从显示中删除。似乎observeEvent没有检测到自行取消选中该框。
    猜你喜欢
    • 2021-05-18
    • 2018-11-09
    • 2020-08-20
    • 1970-01-01
    • 1970-01-01
    • 2019-12-26
    • 2020-05-02
    • 2019-08-18
    • 2021-05-01
    相关资源
    最近更新 更多