R Shiny:使用ShinyWidget pickerInput

时间:2018-06-20 02:19:53

标签: r shiny leaflet

我正在使用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则不起作用。

任何建议都将受到欢迎,因为我无法找到任何与Leaflet一起使用pickerInput的类似示例。

1 个答案:

答案 0 :(得分: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
 }