Leaflet和R

时间:2016-12-07 15:24:07

标签: r leaflet geospatial

我非常兴奋Leaflet与R结合的空间能力,但我非常需要在地图上移动标记和/或绘制路径的可能性。据我所知,Leaflet R软件包缺少此选项,尽管原始的Java版本可能会被强制使用。你有什么想法吗?

1 个答案:

答案 0 :(得分:1)

问题是相当高级的,但话虽如此,有一个答案here提供了一个解决方案,用于在闪亮的应用程序中绘制地图上的点。

如果要在点之间添加线条并显示行进的路线,请使用addPolylines()。示例:

library(shiny)
library(dplyr)
library(leaflet)

travel <- data.frame("time" = c("6/20/17 13:32", "6/20/17 13:33", "6/20/17 13:34", "6/20/17 13:35", "6/20/17 13:36", "6/20/17 13:37"),
             "lat" = c(59.313833, 59.312333, 59.309897, 59.307728, 59.300728, 59.298184),
             "lon" = c(18.070431, 18.07431, 18.085347, 18.076543, 18.080761, 18.076176),
             stringsAsFactors = F) %>%
          mutate(
            time = as.POSIXct(time, format = "%m/%d/%y %H:%M")
          )

# define ui with slider and animation control for time
ui <- fluidPage(
          sliderInput(inputId = "time", label = "Time", min = min(travel$time), 
          max = max(travel$time),
          value = min(travel$time),
          step=60, # set to increment by 60 seconds, adjust appropriately
          animate=T),
          leafletOutput("mymap")
)

server <- function(input, output, session) {
    points <- reactive({
        travel %>% 
            filter(time == input$time)
    })

    history <- reactive({
        travel %>%
            filter(time <= input$time)
    })

    output$mymap <- renderLeaflet({
        leaflet() %>%
            addTiles() %>%
            addMarkers(lng = ~lon,
                       lat = ~lat,
                       data = points()) %>%
            addMarkers(lng = ~lon,
                       lat = ~lat,
                       data = history()) %>%
            addPolylines(lng = ~lon,
                         lat = ~lat,
                         data = history())
   })
}

shinyApp(ui, server)