title: Split code of one leaflet map (so that input updates of one part does not affect other part of code)
body: <p>Is it possible to split the code of a map so that a part of the map only updates if it's own input is changed?</p>
  <p>In the reproducible example below, when selecting the &quot;toner&quot; tile and selecting a new station, the whole leaflet map is executed again because <code>addLegend</code> needs to be updated. Which makes the tile jump back to &quot;OSM (default)&quot; tile. I would like to stay at the tile I selected when I select other stations.</p>
  <pre><code>library(leaflet)
  library(shiny)
  library(dplyr)
  
  pal &lt;- colorFactor(
    palette = &quot;YlGnBu&quot;,
    domain = quakes$stations
  )
  
  ui &lt;- fluidPage(
    sidebarLayout(
      sidebarPanel(
        checkboxGroupInput(&quot;stations&quot;, 
                    &quot;Choose a station&quot;,
                    choices=sort(unique(quakes$stations)),
                    selected = c(10, 11))
      ),
      mainPanel(
        leafletOutput(&quot;map&quot;)
      )
    )
  )
  
  server &lt;- function(input, output) {
    points &lt;- reactive({
      quakes %&gt;%
        filter(stations %in% input$stations)
    })
    
    output$map &lt;- renderLeaflet({
      leaflet(quakes) %&gt;%
        addTiles(group = &quot;OSM (default)&quot;) %&gt;%
        addProviderTiles(providers$Stamen.Toner, group = &quot;Toner&quot;) %&gt;%
        addLayersControl(
          baseGroups = c(&quot;OSM (default)&quot;, &quot;Toner&quot;),
          options = layersControlOptions(collapsed = FALSE)) %&gt;%
        addLegend(&quot;Legend&quot;, position = &quot;topleft&quot;, pal = pal, values = input$stations)
    })
    
    observe({
      if(nrow(points()) == 0) {
        leafletProxy(&quot;map&quot;, data = points()) %&gt;%
          clearMarkers()
      } else {
        leafletProxy(&quot;map&quot;, data = points()) %&gt;%
          clearMarkers() %&gt;%
          addCircleMarkers(radius = 2)
      }
    })
  }
  
  shinyApp(ui, server)
  </code></pre>
  <p>I tried several things, including adding <code>addLegend</code> to the <code>else</code> statement, but that does not go well. I'm new to leaflet/shiny, moving <code>addLegend</code> seemed most logic to me. I really appreciate any suggestions!</p>