0

I am developing a dashboard in Rmarkdown, Flexdashboard and Shiny. At the moment I am using Leaflet to map several points and I have a shiny input, which when pressing a button selects a certain point and zooms it.

I would like to know if there is a way to control the zoom speed.

An example in pictures:

Initial state of the map, the points are in blue.

map1

Panel where the user selects the point and the button executes the filtering and zooming.

inputs1

In this image I show the zoom to the selected point, which is very fast.

map2

I leave some example code and I remain attentive to your comments. Thank you.

{r chunk, echo = false}

leafletOutput(outputId = "mapa1")

# DATA FILTERED BY USER INPUT (1 point)
rbdFiltered <- reactive({
  misDatos()[misDatos()$rdb == input$rbd, ]
})

# LEAFLET OBJECT WITH ALL POINTS
myMap <- reactive({
  input$params
  leaflet() %>% 
    addTiles() %>% 
    addCircleMarkers(data = misDatos(), lng = ~ longitud, lat = ~ latitud, radius = 3, group = "myMarkers") %>% 
    setView(lng = -70.560946, lat = -33.592340, zoom = 13) # ZOOM SET HERE
})


# OBSERVE EVENT (Select a point)

observeEvent(input$BT_select_rbd, {
  leafletProxy(mapId = "mapa1") %>% clearGroup(group = "myMarkers") %>% 
    addCircleMarkers(data = rbdFiltered(), lng = ~ longitud, lat = ~ latitud, radius = 3, group = "myMarkers") %>% 
# HERE A ZOOM TO THE SELECTED POINT IS PROCESSED, IS IT POSSIBLE TO CONTROL THE SPEED?
    setView(lng = rbdFiltered()$longitud, lat = rbdFiltered()$latitud, zoom = 17) 
})

# OBSERVE EVENT (Show all points)

observeEvent(input$BT_todo_rbd, {
  leafletProxy(mapId = "mapa1") %>% clearGroup(group = "myMarkers") %>% 
    addCircleMarkers(data = misDatos(), lng = ~ longitud, lat = ~ latitud, radius = 3, group = "myMarkers") %>% 
    setView(lng = -70.560946, lat = -33.592340, zoom = 13) # WE RETURN TO ORIGINAL ZOOM 

})

output$mapa1 <- renderLeaflet({myMap()})

Regards EDITED: Images

Wolkuz
  • 83
  • 9

1 Answers1

3

Instead of using setView() in your Shiny events, try using leaflet::flyTo().

SEAnalyst
  • 1,077
  • 8
  • 15
  • 1
    Perfect @SEAnalyst ! To control the speed you have to set it in options: `flyTo(lng = lng, lat = lat, zoom = zoom, options = list(duration = 3))`. From [leafletjs documentation](https://leafletjs.com/reference-1.7.1.html#pan-options) – Wolkuz Jun 04 '21 at 17:08