【问题标题】:Can't filter markers in RShiny based on user input无法根据用户输入过滤 RShiny 中的标记
【发布时间】:2020-02-21 17:53:48
【问题描述】:

我的 RShiny 应用程序无法过滤传单地图上的标记时遇到问题。我究竟做错了什么?我无法确定为什么会发生这种情况。我做了同样的事情,但在其他方面,例如,使用基于 input$df 的开关来切换传单代理中的数据集。

这是我的全部代码

用户界面:

ui <- fluidPage(
theme = shinytheme("superhero"),
tags$head(
    includeCSS("MarkerCluster.Default.css", "MarkerCluster.css"),

    includeScript("leaflet.markercluster-src.js"),

),

titlePanel("Map App"),
sidebarLayout(
    position = "right",
    sidebarPanel(
        h3("Options"),
        selectInput(
            "df",
            h5("Display facilities"),
            choices = list(
                "All" = 3,
                "Empty" = 2,
                "Non-empty" = 1
            ),
            selected = 3
        ),


    ),
    mainPanel(
        h3("Map demo with MarkerClusters"),
        tabsetPanel(
            type = "tabs",
            tabPanel(
                "Map",
                leafletOutput("map1", width = "100%", height = "764px"),

            ),
            tabPanel("Data", h4("Showing first 100 rows"), tableOutput("data"))
        )




    )
)
)

服务器:

server <- function(input, output) {


output$map1 <- renderLeaflet({
    leaflet() %>%
        addTiles(attribution = "Map Demo") %>%
        setView(-98.5795, 39.828175, zoom = 3)
})

output$data <- renderTable({
    ds_comp2[1:100, ]
})





observe({
    filter <- reactive({

            switch(input$df,
                   "1" = ds_comp2[ds_comp2$empty == F,],
                   "2" = ds_comp2[ds_comp2$empty == T,],
                   "3" = ds_comp2[,]


            )})

    proxy <- leafletProxy("map1") %>%
                   clearMarkerClusters() %>%
                    clearMarkers() %>%



                   addMarkers(
                       clusterOptions = markerClusterOptions(),
                       data = filter(), 

                       popup = paste(
                           "<b>ZIP code:</b>",
                           ds_comp2$zip,
                           "<br>",
                           "<b>Type:</b>",
                           ds_comp2$type,
                           "<br>",
                           "<b>Group:</b>",
                           ds_comp2$group,
                           "<br>",
                           "<b>Empty?:</b>",!(ds_comp2$empty),
                           "<br>"
                       )
                   )
           })





}

shinyApp(ui = ui, server = server)

编辑:

数据头

 type group empty   zip        lon      lat
1     1     3  TRUE 01913  -70.94279 42.85258
2     0     3  TRUE 92708 -117.96005 33.71688
3     1     3 FALSE 97402 -123.22592 44.04315
4     0     3  TRUE 02109  -71.04829 42.36606
5     0     1 FALSE 92626 -117.90732 33.68341
6     0     2  TRUE 94103 -122.40992 37.77264
7     1     2  TRUE 21801  -75.63245 38.40015
8     0     2  TRUE 10011  -74.00945 40.74650
9     1     2 FALSE 78701  -97.74439 30.27292
10    1     2 FALSE 99019 -117.06447 47.63483

【问题讨论】:

  • 为了让这个可以重现,你能分享一些具体的数据ds_comp2(例如使用dput(head(ds_comp2, 10)))吗?如果可以重现该应用程序,我认为我们可以提出更好的建议(特别是在 observer 中使用 reactive 函数)。
  • @Ben 给你。抱歉,我无法在评论 imgur.com/a/JhYubsl 中将其格式化为表格。我会编辑我的帖子
  • @Ben 我已将其添加到我的帖子中

标签: r shiny leaflet r-leaflet


【解决方案1】:

这似乎对我有用。将leafletProxy 单独放在observe 中,并让filter 成为单独的reactive 块。似乎地图标记是根据过滤数据显示的。让我知道这是否适合您。

server <- function(input, output) {

  output$map1 <- renderLeaflet({
    leaflet() %>%
      addTiles(attribution = "Map Demo") %>%
      setView(-98.5795, 39.828175, zoom = 3)
  })

  output$data <- renderTable({
    ds_comp2[1:100, ]
  })

  filter <- reactive({
      switch(input$df,
             "1" = ds_comp2[ds_comp2$empty == F,],
             "2" = ds_comp2[ds_comp2$empty == T,],
             "3" = ds_comp2[,]
      )
  })

  observe({
    proxy <- leafletProxy("map1") %>%
       clearMarkerClusters() %>%
       clearMarkers() %>%
       addMarkers(
         clusterOptions = markerClusterOptions(),
         data = filter(),
          popup = paste(
            "<b>ZIP code:</b>",
            ds_comp2$zip,
            "<br>",
            "<b>Type:</b>",
            ds_comp2$type,
            "<br>",
            "<b>Group:</b>",
            ds_comp2$group,
            "<br>",
            "<b>Empty?:</b>",!(ds_comp2$empty),
            "<br>"
          )
      )
  })
}

【讨论】:

  • 感谢您的代码。可悲的是,它对我不起作用。我认为这里的问题在于 MarkerCluster js 插件:(
  • 我确实注释掉了tags$head - 如果你这样做,它会起作用吗?否则,我所做的唯一更改是在上面复制的 server 方法中。当你说它不起作用时,发生了什么?一个错误?更少的标记?其他?
  • 你是对的;我没有注意到您注释掉了弹出窗口。我只是在没有的情况下尝试了它,它奏效了!非常感谢您的帮助
  • 太棒了!如果合适,请随意投票或接受答案。
猜你喜欢
  • 1970-01-01
  • 2021-01-18
  • 2018-11-05
  • 2016-11-19
  • 1970-01-01
  • 2017-09-24
  • 1970-01-01
  • 2022-01-19
  • 2022-01-15
相关资源
最近更新 更多