Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有闪亮-clearMarkers()的交互式传单,并设置新传单_R_Shiny_Shinydashboard_R Leaflet - Fatal编程技术网

带有闪亮-clearMarkers()的交互式传单,并设置新传单

带有闪亮-clearMarkers()的交互式传单,并设置新传单,r,shiny,shinydashboard,r-leaflet,R,Shiny,Shinydashboard,R Leaflet,我想创建一个互动传单地图与闪亮。创建工作正常,但尽管在selectInput()和单击“go!”时进行了更改,但不会删除旧标记,也不会设置新标记。你能帮帮我吗 操作系统: Windows 10 64位 斯图迪奥 R版本:R版本4.0.0 geocodes <- data.frame("customer" = c("John", "Ahmet", "Zara"), &

我想创建一个互动传单地图与闪亮。创建工作正常,但尽管在selectInput()和单击“go!”时进行了更改,但不会删除旧标记,也不会设置新标记。你能帮帮我吗

操作系统: Windows 10 64位 斯图迪奥 R版本:R版本4.0.0

geocodes <- data.frame("customer" = c("John", "Ahmet", "Zara"),
                       "longitude" = c(8.71, 8.59, 8.98),
                       "latitude" = c(51.2, 51.3, 51.1))
# UI
ui <- dashboardPage(
  dashboardHeader(title = "CustomerLocation Dashboard"),
  dashboardSidebar(
    
    selectInput("account", label = "Account",
                choices = c(unique(geocodes$customer)),
                multiple = TRUE),
    
    actionButton("go", "Go!")

  ),

  
  dashboardBody(
    tags$style(type = "text/css", "#map {height: calc(100vh - 80px) !important;}"),
    leafletOutput("mymap", height=1000)
  )
)
  

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

  reactiveDf <- reactive({
    if(is.null(input$account)){
      geocodes
    } else{
      geocodes[geocodes$customer %in% input$account,]
    }
  })
  
  
  output$mymap <- renderLeaflet({
    leaflet(geocodes) %>%
      addProviderTiles("OpenStreetMap",
                       group = "OpenStreetMap"
      ) %>%
      addAwesomeMarkers(
        lng = ~longitude,
        lat = ~latitude,
        icon = customIcon,
        clusterOptions = markerClusterOptions(),
        label = ~customer,
        popup = ~popup
      )
  })
  
  
  eventReactive(input$go,{
   leafletProxy("mymap", data = reactiveDf()) %>%
      clearShapes() %>%
      clearPopups() %>%
      clearMarkers() %>%
      addAwesomeMarkers(
        data = reactiveDf(),
        lng = ~longitude,
        lat = ~latitude,
        icon = customIcon,
        clusterOptions = markerClusterOptions(),
        label = ~customer,
        popup = ~popup
      )
  })

}

shinyApp(ui, server)
geocodes%
clearMarkers()%>%
添加体标记(
data=reactiveDf(),
lng=经度,
纬度=~纬度,
图标=自定义图标,
clusterOptions=markerClusterOptions(),
标签=~客户,
弹出=~popup
)
})
}
shinyApp(用户界面、服务器)

我已经确定了三个修复程序:

  • c(unique(geocodes$customer))
    将此输入转换为
    c(1,2,3)
    。当您稍后尝试用c(1,2,3)将客户名称“John”、“Ahmet”和“Zara”子集时,这是不兼容的,并且失败了
  • 改为:

    selectInput("account", label = "Account",
                    choices = unique(geocodes$customer),
                    multiple = TRUE),
    
    output$mymap <- renderLeaflet({
        leaflet(reactiveDf()) %>%
          addProviderTiles("OpenStreetMap",
                           group = "OpenStreetMap"
          ) %>%
          addAwesomeMarkers(
            lng = ~longitude,
            lat = ~latitude,
            icon = customIcon,
            clusterOptions = markerClusterOptions(),
            label = ~customer,
            popup = ~popup
          )
      })
    
  • 不幸的是,我不知道为什么
    proxy()
    中的
    clearMarkers()
    不会像您期望的那样清除标记,而是将原始地图从使用
    geocodes
    渲染更改为
    reactiveDf()
    (如果
    input$account
    为空,它仍然返回
    geocodes
    数据帧)似乎解决了这个问题
  • 编辑:另一个选项是为每个标记层定义一个组,并调用showGroup()和hideGroup()来操纵可视显示的标记

    我希望这有帮助

    output$mymap <- renderLeaflet({
        leaflet(geocodes) %>%
          addProviderTiles("OpenStreetMap",
                           group = "OpenStreetMap"
          ) %>%
          addAwesomeMarkers(
            lng = ~longitude,
            lat = ~latitude,
            icon = customIcon,
            clusterOptions = markerClusterOptions(),
            label = ~customer,
            popup = ~popup
          )
      })
    
    output$mymap <- renderLeaflet({
        leaflet(reactiveDf()) %>%
          addProviderTiles("OpenStreetMap",
                           group = "OpenStreetMap"
          ) %>%
          addAwesomeMarkers(
            lng = ~longitude,
            lat = ~latitude,
            icon = customIcon,
            clusterOptions = markerClusterOptions(),
            label = ~customer,
            popup = ~popup
          )
      })
    
    observeEvent(input$go, {
        if(is.null(input$account)){
          geocodes = geocodes
        } else{
         geocodes = geocodes[geocodes$customer %in% input$account,]
        }
     leafletProxy("mymap") %>%
        clearShapes() %>%
        clearPopups() %>%
        clearMarkers() %>%
        addMarkers(
          data = geocodes,
          lng = ~longitude,
          lat = ~latitude,
          label = ~customer,
        )
      }
    )