1

I am trying to visualise a random walk. Not its path, but actually see the marker moving as it wanders around. Something like this.

I have come with this workaround in which I clear all markers and add them again with the new positions at every step.

library(shiny)
library(leaflet)


df <- data.frame(latitude = 10, longitude = 0)

ui <- fluidPage(
  sliderInput("time", "date", 0,
    1e2,
    value = 1,
    step = 1,
    animate = TRUE
  ),
  leafletOutput("mymap")
)

server <- function(input, output, session) {
  points <- eventReactive(input$time, {
    df$latitude <- df$latitude + rnorm(1)
    df$longitude <- df$longitude + rnorm(1)
    df
  })

  output$mymap <- renderLeaflet({
    leaflet() %>%
      addTiles()
  })

  observe({
    leafletProxy("mymap") %>%
      clearMarkers() %>%
      addMarkers(data = points())
  })
}

shinyApp(ui, server)

But I found a much more neat solution in this method movingMarker. I was wondering if there's a way to implement it using that javascript code.

  • hi, did you check [here](https://stackoverflow.com/questions/41021237/animated-marker-moving-and-or-path-trajectory-with-leaflet-and-r)? – bretauv Mar 08 '20 at 16:04
  • Yes, it's kind of the same workaround I've pasted here. It keeps adding static markers in different positions, but you never see them moving. – Alejandro Jiménez Rico Mar 08 '20 at 16:36

0 Answers0