title: leaflet R, how to make appearance of clustered icon related to statistics of the children?
body: <p>I'd like to customize appearance of clustered markers in Leaflet/Shiny application based on sum of an attribute of child markers.</p>
  
  <p>It is similar to <a href="https://stackoverflow.com/questions/33600021/leaflet-for-r-how-to-customize-the-coloring-of-clusters">this problem</a>, which makes icon color of clusters based on count of children.  What if I want to customize icon based on the sum of magnitude of earthquakes?</p>
  
  <p>With pure javascript application, seems like I should be able to set custom property to individual marker, then access it from <code>iconCreateFunction</code>, as done in <a href="https://github.com/Leaflet/Leaflet.markercluster/blob/master/example/marker-clustering-custom.html" rel="nofollow noreferrer">this example</a>. </p>
  
  <p>But I am adding marker with <code>addCircleMarkers</code> and <code>addMarkers</code> from leaflet for R, and doesn't seem i can add arbitrary attribute to markers being generated.  Code below works but it doesn't if i uncomment two lines (<code>mag = ~mag</code> and <code>sum += markers[i].mag;</code>)</p>
  
  <pre><code>leaflet(quakes) %&gt;% addTiles() %&gt;% addMarkers(
    # mag = ~mag,
    clusterOptions = markerClusterOptions(
    iconCreateFunction=JS("function (cluster) {    
      var markers = cluster.getAllChildMarkers();
      var sum = 0; 
      for (i = 0; i &lt; markers.length; i++) {
      //  sum += markers[i].mag;
        sum += 1;
      }
      return new L.DivIcon({ html: '&lt;div&gt;&lt;span&gt;' + sum + '&lt;/span&gt;&lt;/div&gt;'});
  
    }")
  
    )
  )
  </code></pre>
  
  <p>I thought about using <code>label=</code> option of <code>addMarkers</code>, and then parse it from Javascript.  But the markers accessed with <code>getAllChildMarkers()</code> on marker cluster in JS does not seem to have <code>label</code> property.</p>
  
  <p>I also thought about passing a dataframe from R to leaflet(JS), somehow, maybe <a href="https://shiny.rstudio.com/articles/communicating-with-js.html" rel="nofollow noreferrer">like this example</a>, or <a href="https://shiny.rstudio.com/articles/js-send-message.html" rel="nofollow noreferrer">this</a> ...?  </p>