Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zoom leaflet map to default in Rshiny

Tags:

r

leaflet

shiny

Is there anyway to add a layer control or button to reset map and go back the initial position. For example, when in the you are exploring a map and zooming in, and then you want to zoom out to get back to the initial stage.

library(shiny)
library(leaflet)

ui <- fluidPage(
  leafletOutput("mymap")

)

server <- function(input, output, session) {

outline <- quakes[chull(quakes$long, quakes$lat),]

output$mymap <- renderLeaflet({ leaflet(quakes) %>%
  # Base groups
  addTiles(group = "OSM (default)") %>%
  addProviderTiles("Stamen.Toner", group = "Toner") %>%
  addProviderTiles("Stamen.TonerLite", group = "Toner Lite") %>%
  # Overlay groups
  addCircles(~long, ~lat, ~10^mag/5, stroke = F, group = "Quakes") %>%
  addPolygons(data = outline, lng = ~long, lat = ~lat,
              fill = F, weight = 2, color = "#FFFFCC", group = "Outline") %>%
  # Layers control
  addLayersControl(
    baseGroups = c("OSM (default)", "Toner", "Toner Lite"),
    overlayGroups = c("Quakes", "Outline"),
    options = layersControlOptions(collapsed = FALSE)
    )

  })
}

shinyApp(ui, server)
like image 903
M.Qasim Avatar asked Oct 01 '15 21:10

M.Qasim


People also ask

How to enable fractional zoom in leaflet?

A feature introduced in Leaflet 1.0.0 was the concept of fractional zoom . Before this, the zoom level of the map could be only an integer number ( 0, 1, 2, and so on); but now you can use fractional numbers like 1.5 or 1.25. Fractional zoom is disabled by default. To enable it, use the map’s zoomSnap option .

How do I change the zoom level of a Leaflet map?

A leaflet map has several ways to control the zoom level shown, but the most obvious one is setZoom(). For example, map.setZoom(0); will set the zoom level of map to 0.

How do I add a Leaflet map to a shiny app?

Most Shiny output widgets are incorporated into an app by including an output (e.g. plotOutput) for the widget in the UI definition, and using a render function (e.g. renderPlot) in the server function. Leaflet maps are no different; in the UI you call leafletOutput, and on the server side you assign a renderLeaflet call to the output.

How do I use the map options in zoomsnap?

There is another important map option related to zoomSnap: the zoomDelta option . This controls how many zoom levels to zoom in/out when using the zoom buttons (from the default L.Control.Zoom ) or the + / - keys in your keyboard. For the mousewheel zoom, the wheelPxPerZoomLevel option controls how fast the mousewheel zooms in or out.


1 Answers

You can do this without any javascript code by using the leafletProxy and the setView functions to change the map when the a button is clicked.

Here's an example:

library(shiny)
library(leaflet)

ui <- fluidPage(
  leafletOutput("mymap"),
  actionButton("reset_button", "Reset view")

)

server <- function(input, output, session) {
  initial_lat = -23.079
  initial_lng = 178.15
  initial_zoom = 4

  output$mymap <- renderLeaflet({ leaflet(quakes) %>% 
                                    setView(lat = initial_lat, lng = initial_lng, zoom = initial_zoom) %>%
                                    addTiles(group = "OSM (default)") %>%
                                    addProviderTiles("Stamen.Toner", group = "Toner") %>%
                                    addProviderTiles("Stamen.TonerLite", group = "Toner Lite")})                                 


  observe({
    input$reset_button
    leafletProxy("mymap") %>% setView(lat = initial_lat, lng = initial_lng, zoom = initial_zoom)
  })
}

shinyApp(ui, server)
like image 120
NicE Avatar answered Sep 26 '22 04:09

NicE