How to get the zoom level from the leaflet map in R/shiny?
<p>I create a map using leaflet package in Shiny which have a <code>selectInput</code> to allow user to select from a site list. The site list also adds into leaflet as markers.</p>

<p>When user selects a new site, I want to recenter map into the selected site without change the zoom level. The <code>setView</code> function can be called to set center points, but has to specify the zoom level. </p>

<p>Is it possible to get the zoom level of leaflet map which can be used in the <code>setView</code> function?</p>

<p>This is a minimum example to play with my question with reset zoom level.</p>

<pre><code>library(shiny)
library(leaflet)

df &lt;- data.frame(
    site = c('S1', 'S2'),
    lng = c(140, 120),
    lat = c(-20, -30), 
    stringsAsFactors = FALSE)

# Define UI for application that draws a histogram
ui &lt;- shinyUI(fluidPage(
    selectInput('site', 'Site', df$site),
    leafletOutput('map')

))

server &lt;- shinyServer(function(input, output, session) {

    output$map &lt;- renderLeaflet({
        leaflet() %&gt;%
            addTiles() %&gt;% 
            setView(lng = 133, lat = -25,  zoom = 4) %&gt;% 
            addMarkers(lng = df$lng, lat = df$lat)
    })

    observe({
        req(input$site)
        sel_site &lt;- df[df$site == input$site,]
        isolate({
            leafletProxy('map') %&gt;%
                setView(lng = sel_site$lng, lat = sel_site$lat, zoom = 4)
        })
    })
})

shinyApp(ui = ui, server = server)
</code></pre>

<p>PS: when you play with these codes, please adjust zoom level before selecting a new site.</p>

<p>Thanks of any suggestions.</p>