title: In shiny, how to fix (lock) leaflet map view zoom and center?
body: <p>I am building a similar app to this <a href="https://github.com/ashtondrew/pyDataDemo/blob/master/3_pyDataApp.R" rel="nofollow noreferrer">one</a>. On the map, if you zoom in and then change the slider/input, the zoom level resets to the default automatically. I would like to render new instances of the map without changing the zoom level until the user changes it back. Ideally, I will add a button to reset the zoom to the original setting.</p>
  
  <p>I looked at these posts: <a href="https://stackoverflow.com/questions/34985889/how-to-get-the-zoom-level-from-the-leaflet-map-in-r-shiny">1</a>, <a href="https://groups.google.com/forum/#!msg/shiny-discuss/T4j5VkkTnVk/Crvzjpz6DAAJ" rel="nofollow noreferrer">2</a>, and <a href="https://stackoverflow.com/questions/28393310/how-to-prevent-leaflet-map-from-resetting-zoom-in-shiny-app">3</a>.</p>
  
  <p>The code in the 3rd link made sense to me but still didn't work. Thier <a href="https://github.com/nikolai-b/hackMyRoute/commit/9a78c46c088e0887a7463e14767c9d0c7b19de3f" rel="nofollow noreferrer">code</a>, based on the comment, should have fixed the zoom issue but not the centering - neither worked for me. Below, I modified the <a href="https://github.com/ashtondrew/pyDataDemo/blob/master/3_pyDataApp.R" rel="nofollow noreferrer">original</a> app to be as close as possible to my app. I also implemented two changes in an attempt to achieve the desired map view behaviour - I added two reactive functions: zoom and center. Here is the modified repex:</p>
  
  <pre><code>library(shiny)
  library(ggplot2)
  library(plotly)
  library(leaflet)
  
  qDat &lt;- quakes
  
  ui &lt;- fluidPage(
    titlePanel("pyData Shiny Demo"),
    sidebarLayout(
      sidebarPanel(
        h3("Fiji Earthquake Data"),
        selectInput("select01", "Select earthquakes based on:",
                    choices=c("Magnitude"="mag",
                              "Depth"="depth"),
                    selected="mag"),
        conditionalPanel(condition="input.select01=='mag'",
                         sliderInput("sld01_mag",
                                     label="Show earthquakes of magnitude:", 
                                     min=min(qDat$mag), max=max(qDat$mag),
                                     value=c(min(qDat$mag),max(qDat$mag)), step=0.1)
        ),
        conditionalPanel(condition="input.select01=='depth'",
                         sliderInput("sld02_depth",
                                     label="Show earthquakes of depth:", 
                                     min=min(qDat$depth), max=max(qDat$depth),
                                     value=c(min(qDat$depth),max(qDat$depth)), step=5)
        ),
        plotlyOutput("hist01")
  
      ),
      mainPanel(
        leafletOutput("map01"),
        dataTableOutput("table01")
      )
    )
  )
  
  server &lt;- shinyServer(function(input, output) {
  
    qSub &lt;- reactive({
      if (input$select01=="mag"){
        subset &lt;- qDat[qDat$mag&gt;=input$sld01_mag[1] &amp; qDat$mag&lt;=input$sld01_mag[2],]
      }else{
        subset &lt;- qDat[qDat$depth&gt;=input$sld02_depth[1] &amp; qDat$depth&lt;=input$sld02_depth[2],]
      }
      subset
    })
  
    output$hist01 &lt;- renderPlotly({
      ggplot(data=qSub(), aes(x=stations))+
        geom_histogram(binwidth=5)+
        xlab("Number of Reporting Stations")+ 
        xlim(min(qDat$stations), max(qDat$stations))+
        ylab("Count")+
        ggtitle("Earthquakes near Fiji")
    })
  
    output$table01 &lt;- renderDataTable({
      qSub()
    })
  
    zoom &lt;- reactive({
      ifelse(is.null(input$map01_zoom),3,input$map01_zoom)
    })
  
    center &lt;- reactive({
      ifelse(is.null(input$map01_bounds),
             c(179.462, -20.64275),
             c((input$map01_bounds$bounds$north + input$map01_bounds$bounds$south)/2.0, 
               (input$map01_bounds$bounds$east + input$map01_bounds$bounds$west)/2.0))
    })
  
  
    pal &lt;- colorNumeric("YlOrRd", domain=c(min(quakes$mag), max(quakes$mag)))
  
    output$map01 &lt;- renderLeaflet({
    leaflet(data=qSub()) %&gt;% 
        addTiles() %&gt;%
        addLegend("bottomright", pal = pal, values = ~mag,
                  title = "Earthquake Magnitude",
                  opacity = 1)
    })
  
    observe({
  
      leafletProxy("map01") %&gt;%
        clearShapes() %&gt;%
        #setView(lng = 179.462, lat =  -20.64275, zoom = 3) %&gt;%
        setView(lng = center()[1],
                lat = center()[2],
                zoom = zoom()) %&gt;%
        addCircleMarkers(
          data=qSub(),
          radius = 2,
          color = ~pal(mag),
          stroke = FALSE, fillOpacity = 1, popup=~as.character(mag))
    })
  
  })
  
  shinyApp(ui = ui, server = server)
  </code></pre>
  
  <p>Any tips on how to achieve this? </p>
  
  <p>Thanks!</p>