2

I am very excited the spatial abilities of Leaflet combined with R but I badly need the possibility to move around markers and/or draw paths over maps. As far as I see the Leaflet R package lacks this option albeit the original Java version could be forced this way. Do you have any idea?

5
  • It is possible with leaflet.extras package (currently only on github) github.com/bhaskarvk/leaflet.extras. An example can be found here rpubs.com/bhaskarvk/leaflet-draw
    – TimSalabim
    Commented Dec 7, 2016 at 15:27
  • Sorry, I just see "Draw Features on Map" ability added here. Where is any animation on the site above exactly?
    – Hendrik
    Commented Dec 7, 2016 at 15:37
  • What exactly do you mean with animation? Time animation?
    – TimSalabim
    Commented Dec 7, 2016 at 15:38
  • I mean that I have a trip recorded (long, lat, timestamp sequence) and I'd like to replay it over a map.
    – Hendrik
    Commented Dec 7, 2016 at 15:43
  • This is in the making github.com/bhaskarvk/leaflet.extras/issues/34. Maybe take to opportunity to add your thoughts to the issue.
    – TimSalabim
    Commented Dec 7, 2016 at 15:46

1 Answer 1

2

The question is quite high-level, but that being said, there is an answer here that provides a solution for drawing points on a map in a shiny app.

If you want to add lines between points and show the route traveled, use addPolylines(). Example:

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)

Not the answer you're looking for? Browse other questions tagged or ask your own question.