R - 闪亮 - 地图上的复制标记与传单

时间:2017-11-22 13:41:03

标签: r shiny leaflet

我的闪亮App中有一张地图。

我尝试按照数据框的一列过滤地图上的标记。

我的数据:

remorque       time.stamp      lat      long geolocalisation maintenance temperature appairage
1        21/11/2017 10:36 48.86272 2.2875920          OnMouv        noir                      
2        21/11/2017 10:36 43.60776 1.4421606       StartMouv                   rouge          
3        21/11/2017 10:36 46.58619 0.3388710          OnMouv                   rouge          
4        21/11/2017 10:36 45.76695 3.0556216            Life                  orange          
5        21/11/2017 10:36 45.14555 1.4751652         EndMouv                             rouge
6        21/11/2017 10:36 46.81157 1.6936336            Life                  orange          
7        21/11/2017 10:36 47.36223 0.6751146          alerte                             rouge
8        21/11/2017 10:36 47.36032 1.7441244       StartMouv                                  
9        21/11/2017 10:36 48.85333 1.8215332       StartMouv                                  
10       21/11/2017 10:36 48.84429 1.7913208          alerte                                  
11       21/11/2017 10:36 48.81356 1.6759643         EndMouv   

在ui.R中我添加:

selectInput("geolocalisation", "Géolocalisation :",
                          choice = list("Tous" = "tous",
                                        "OnMouv" = "OnMouv",
                                        "StartMouv" = "StartMouv",
                                        "EndMouv" = "EndMouv",
                                        "Life" = "Life",
                                        "Perte ou Vol" = "alerte"))

在server.R中我做了:

output$map <- renderLeaflet({
# Use leaflet() here, and only include aspects of the map that
# won't need to change dynamically (at least, not unless the
# entire map is being torn down and recreated).
leaflet() %>% 
  addTiles() 
})

zerg <-reactive({
test<-ifelse(input$geolocalisation=="OnMouv", return(data_moment[data_moment$geolocalisation=="OnMouv",]),
             ifelse(input$geolocalisation=="StartMouv", return(data_moment[data_moment$geolocalisation=="StartMouv",]),
                    ifelse(input$geolocalisation=="EndMouv", return(data_moment[data_moment$geolocalisation=="EndMouv",]),
                           ifelse(input$geolocalisation=="Life", return(data_moment[data_moment$geolocalisation=="Life",]),
                                  ifelse(input$geolocalisation=="alerte", return(data_moment[data_moment$geolocalisation=="alerte",]),
                                         return(data_moment))))))
return(test)
})


observe({

dataset<- zerg()

leafletProxy("map", data = dataset) %>%
  clearMarkers() %>%
  addMarkers(~long, ~lat,clusterOptions = markerClusterOptions(), icon = load_icons(dataset),
             popup= ~paste("<h4><font color='#2B547E'><b>Remorque : </font></b>",remorque,"</h4><h4><font color='#2B547E'>Latitude : </font>",lat,"</h4><h4><font color='#2B547E'>Longitude : </font>",long,"</h4>"),
             label = ~paste("Remorque : ",remorque,"::: Latitude : ",lat,"::: Longitude : ",long))

})

当我浏览应用程序时,会显示所有标记(这很好)。当我做出选择时,它会添加标记,而不是选择标记。只需添加重复的新内容。

你知道为什么吗?

1 个答案:

答案 0 :(得分:1)

所以秘诀就是使用clearClusterMarkers()代替clearMarkers(),因为在定义参数clusterOptions = markerClusterOptions时将标记调整为聚类标记。

以下是工作单个文件app.R

的长形式
library(shiny)
library(leaflet)

data_moment <- read.table(
text =
"remorque       time.stamp      lat      long geolocalisation
1        21/11/2017 10:36 48.86272 2.2875920          OnMouv 
2        21/11/2017 10:36 43.60776 1.4421606       StartMouv 
3        21/11/2017 10:36 46.58619 0.3388710          OnMouv
4        21/11/2017 10:36 45.76695 3.0556216            Life
5        21/11/2017 10:36 45.14555 1.4751652         EndMouv
6        21/11/2017 10:36 46.81157 1.6936336            Life
7        21/11/2017 10:36 47.36223 0.6751146          alerte
8        21/11/2017 10:36 47.36032 1.7441244       StartMouv                                  
9        21/11/2017 10:36 48.85333 1.8215332       StartMouv                                  
10       21/11/2017 10:36 48.84429 1.7913208          alerte                                  
11       21/11/2017 10:36 48.81356 1.6759643         EndMouv", header = TRUE)

ui <- fluidPage(

   titlePanel("A map"),

   sidebarLayout(
      sidebarPanel(
          selectInput("geolocalisation", "Géolocalisation :",
                      choice = list("Tous" = "tous",
                                    "OnMouv" = "OnMouv",
                                    "StartMouv" = "StartMouv",
                                    "EndMouv" = "EndMouv",
                                    "Life" = "Life",
                                    "Perte ou Vol" = "alerte"))
      ),

      mainPanel(
         leafletOutput("map")
      )
   )
)

server <- function(input, output) {
    output$map <- renderLeaflet({
        # Use leaflet() here, and only include aspects of the map that
        # won't need to change dynamically (at least, not unless the
        # entire map is being torn down and recreated).
        leaflet() %>% 
            addTiles() %>%
            # I added this so we don't have to zoom in from outer space each time
            setView(lng = 2.3522, lat = 48.8566, zoom = 5)
    })

    zerg <-reactive({
        test<-ifelse(input$geolocalisation=="OnMouv", return(data_moment[data_moment$geolocalisation=="OnMouv",]),
                     ifelse(input$geolocalisation=="StartMouv", return(data_moment[data_moment$geolocalisation=="StartMouv",]),
                            ifelse(input$geolocalisation=="EndMouv", return(data_moment[data_moment$geolocalisation=="EndMouv",]),
                                   ifelse(input$geolocalisation=="Life", return(data_moment[data_moment$geolocalisation=="Life",]),
                                          ifelse(input$geolocalisation=="alerte", return(data_moment[data_moment$geolocalisation=="alerte",]),
                                                 return(data_moment))))))
        return(test)
    })


    observe({

        data_set <- zerg

        leafletProxy("map", data = zerg()) %>%
            # this is the only change you really need
            clearMarkerClusters() %>%
            addMarkers(~long, ~lat, clusterOptions = markerClusterOptions(),
                       popup= ~paste("<h4><font color='#2B547E'><b>Remorque : </font></b>",remorque,"</h4><h4><font color='#2B547E'>Latitude : </font>",lat,"</h4><h4><font color='#2B547E'>Longitude : </font>",long,"</h4>"),
                       label = ~paste("Remorque : ",remorque,"::: Latitude : ",lat,"::: Longitude : ",long))

    })
}

shinyApp(ui = ui, server = server)