title: Display layers at certain zoom levels in R Leaflet
body: <p>I am working on an interactive map with the R package "leaflet".</p>
  
  <p>I would like to change automatically the visible layers depending on the zoom level.</p>
  
  <p>For example, I would like to have a polygon layer disappearing when zooming in, replaced by a points layer. Something like this : <a href="https://tree-map.nycgovparks.org/" rel="nofollow noreferrer">https://tree-map.nycgovparks.org/</a></p>
  
  <p>I've been trying many different tricks and exploring in details the help from the "leaflet" and "leaflet.extras" packages, but could not find anything doing that.</p>
  
  <p>I also found something straight from leaflet but it does not seem to be reproducible under R :
  <a href="https://stackoverflow.com/questions/58629387/setting-zoom-level-for-layers-in-leaflet">Setting zoom level for layers in leaflet</a></p>
  
  <p>I tried to use the options minZoom and maxZoom from markerOptions, but it does not appear to do what I want.</p>
  
  <p>Here is my code for this example :</p>
  
  <pre><code>
  require(spData)
  require(leaflet)
  require(sf)
  
  # loading shapes of countries from the package spData
  
  data(world)
  world &lt;- st_read(system.file("shapes/world.gpkg", package="spData"))
  
  # creating a sf objet with oceanian countries boundaries
  
  oceania &lt;- world[world$continent=="Oceania",]
  
  #loading points events from the quakes dataset
  
  data(quakes)
  
  #Creating a leaflet objet with points and polygons
  
  leaflet() %&gt;%
    addProviderTiles(providers$CartoDB.Positron) %&gt;%
    addCircleMarkers(lng=quakes$long,
                     lat=quakes$lat,
                     col="blue",
                     radius=3,
                     stroke=FALSE,
                     fillOpacity = 0.7,
                     options = markerOptions(minZoom=15, maxZoom=20)) %&gt;%
    addPolygons(data= oceania,
                col="red")
  </code></pre>
  
  <p>It gives me the expected layers with the expected background from openstreetmap, but the minZoom and maxZoom arguments don't change anything. I expected the points layer to only appear between zoom levels 15 and 20, but it does not work like this, it seems.</p>
  
  <p><a href="https://i.stack.imgur.com/9fXMJ.jpg" rel="nofollow noreferrer">Image from the viewer</a></p>