title: Leaflet Legend for Custom Markers in R
body: <p>I have an R Shiny app that uses Leaflet to create an interactive map.  On this map, a categorical variable is used to designate different kinds of points and is visualized using custom markers (different icons, depending on the factor level).</p>
  
  <p>What I would like to do is add a legend to the plot, but have the legend show the various marker icons instead of solid colours.  The <a href="http://rstudio.github.io/leaflet/legends.html" rel="noreferrer">legends tutorial</a> does not cover this.</p>
  
  <p>I have come across another <a href="https://stackoverflow.com/questions/37701211/custom-legend-image-as-legend-in-leaflet-map">SO answer that seems to solve this</a> - but it was done in JavaScript and I'm not sure how to translate it/if it can be translated to work in R. Anyone know how to accomplish this?</p>
  
  <p>A basic reproducible example:</p>
  
  <pre><code>library(leaflet)
  
  # Sample Data
  data(quakes)
  quakes &lt;- quakes[1:10,]
  
  # Choose Icon:
  leafIcons &lt;- icons(
    iconUrl = ifelse(quakes$mag &lt; 4.6,
                     "http://leafletjs.com/docs/images/leaf-green.png",
                     "http://leafletjs.com/docs/images/leaf-red.png"
    ),
    iconWidth = 38, iconHeight = 95,
    iconAnchorX = 22, iconAnchorY = 94)
  
  # Produce Map:
  leaflet(data = quakes) %&gt;% addTiles() %&gt;%
    addMarkers(~long, ~lat, icon = leafIcons)
  </code></pre>