leaflet R, how to make appearance of clustered icon related to statistics of the children?










1















I'd like to customize appearance of clustered markers in Leaflet/Shiny application based on sum of an attribute of child markers.



It is similar to this problem, 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?



With pure javascript application, seems like I should be able to set custom property to individual marker, then access it from iconCreateFunction, as done in this example.



But I am adding marker with addCircleMarkers and addMarkers 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 (mag = ~mag and sum += markers[i].mag;)



leaflet(quakes) %>% addTiles() %>% addMarkers(
# mag = ~mag,
clusterOptions = markerClusterOptions(
iconCreateFunction=JS("function (cluster)
var markers = cluster.getAllChildMarkers();
var sum = 0;
for (i = 0; i < markers.length; i++)
// sum += markers[i].mag;
sum += 1;

return new L.DivIcon( html: '<div><span>' + sum + '</span></div>');

")

)
)


I thought about using label= option of addMarkers, and then parse it from Javascript. But the markers accessed with getAllChildMarkers() on marker cluster in JS does not seem to have label property.



I also thought about passing a dataframe from R to leaflet(JS), somehow, maybe like this example, or this ...?










share|improve this question




























    1















    I'd like to customize appearance of clustered markers in Leaflet/Shiny application based on sum of an attribute of child markers.



    It is similar to this problem, 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?



    With pure javascript application, seems like I should be able to set custom property to individual marker, then access it from iconCreateFunction, as done in this example.



    But I am adding marker with addCircleMarkers and addMarkers 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 (mag = ~mag and sum += markers[i].mag;)



    leaflet(quakes) %>% addTiles() %>% addMarkers(
    # mag = ~mag,
    clusterOptions = markerClusterOptions(
    iconCreateFunction=JS("function (cluster)
    var markers = cluster.getAllChildMarkers();
    var sum = 0;
    for (i = 0; i < markers.length; i++)
    // sum += markers[i].mag;
    sum += 1;

    return new L.DivIcon( html: '<div><span>' + sum + '</span></div>');

    ")

    )
    )


    I thought about using label= option of addMarkers, and then parse it from Javascript. But the markers accessed with getAllChildMarkers() on marker cluster in JS does not seem to have label property.



    I also thought about passing a dataframe from R to leaflet(JS), somehow, maybe like this example, or this ...?










    share|improve this question


























      1












      1








      1








      I'd like to customize appearance of clustered markers in Leaflet/Shiny application based on sum of an attribute of child markers.



      It is similar to this problem, 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?



      With pure javascript application, seems like I should be able to set custom property to individual marker, then access it from iconCreateFunction, as done in this example.



      But I am adding marker with addCircleMarkers and addMarkers 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 (mag = ~mag and sum += markers[i].mag;)



      leaflet(quakes) %>% addTiles() %>% addMarkers(
      # mag = ~mag,
      clusterOptions = markerClusterOptions(
      iconCreateFunction=JS("function (cluster)
      var markers = cluster.getAllChildMarkers();
      var sum = 0;
      for (i = 0; i < markers.length; i++)
      // sum += markers[i].mag;
      sum += 1;

      return new L.DivIcon( html: '<div><span>' + sum + '</span></div>');

      ")

      )
      )


      I thought about using label= option of addMarkers, and then parse it from Javascript. But the markers accessed with getAllChildMarkers() on marker cluster in JS does not seem to have label property.



      I also thought about passing a dataframe from R to leaflet(JS), somehow, maybe like this example, or this ...?










      share|improve this question
















      I'd like to customize appearance of clustered markers in Leaflet/Shiny application based on sum of an attribute of child markers.



      It is similar to this problem, 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?



      With pure javascript application, seems like I should be able to set custom property to individual marker, then access it from iconCreateFunction, as done in this example.



      But I am adding marker with addCircleMarkers and addMarkers 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 (mag = ~mag and sum += markers[i].mag;)



      leaflet(quakes) %>% addTiles() %>% addMarkers(
      # mag = ~mag,
      clusterOptions = markerClusterOptions(
      iconCreateFunction=JS("function (cluster)
      var markers = cluster.getAllChildMarkers();
      var sum = 0;
      for (i = 0; i < markers.length; i++)
      // sum += markers[i].mag;
      sum += 1;

      return new L.DivIcon( html: '<div><span>' + sum + '</span></div>');

      ")

      )
      )


      I thought about using label= option of addMarkers, and then parse it from Javascript. But the markers accessed with getAllChildMarkers() on marker cluster in JS does not seem to have label property.



      I also thought about passing a dataframe from R to leaflet(JS), somehow, maybe like this example, or this ...?







      javascript r leaflet






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 '18 at 18:02







      yosukesabai

















      asked Nov 11 '18 at 5:00









      yosukesabaiyosukesabai

      4,28611833




      4,28611833






















          1 Answer
          1






          active

          oldest

          votes


















          1














          Found my answer. Seems like I can put arbitrary property inside options= in addMarker:



          leaflet(quakes) %>% addTiles() %>% addMarkers(
          options = markerOptions(mag = ~mag),
          clusterOptions = markerClusterOptions(
          iconCreateFunction=JS("function (cluster)
          var markers = cluster.getAllChildMarkers();
          var sum = 0;
          for (i = 0; i < markers.length; i++)
          sum += Number(markers[i].options.mag);
          // sum += 1;

          return new L.DivIcon( html: '<div><span>' + sum + '</span></div>');
          ")
          )
          )





          share|improve this answer























          • great, thanks much for sharing (+1 +1)! Could you indicate how to keep the default coloring and shape of the clusters? I tried adding back ,className: 'marker-cluster' at the end of the JS function, but it seems we have to re-specify everything

            – Antoine
            Nov 24 '18 at 11:19












          • you have to pass thing like below, two words, seems like. return new L.DivIcon( html: '<div><span>' + sum + '</span></div>', className: 'marker-cluster marker-cluster-medium', iconSize: new L.Point(40,40)); you need your logic to set if marker is large/small/medium, like the original code was doing

            – yosukesabai
            Nov 25 '18 at 4:18











          • By original code I meant class definition of MarkerClusterGroup, _defaultIconCreateFunction(), found in here or somewhere nearby: github.com/Leaflet/Leaflet.markercluster/blob/master/src/…

            – yosukesabai
            Nov 25 '18 at 4:22










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245984%2fleaflet-r-how-to-make-appearance-of-clustered-icon-related-to-statistics-of-the%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Found my answer. Seems like I can put arbitrary property inside options= in addMarker:



          leaflet(quakes) %>% addTiles() %>% addMarkers(
          options = markerOptions(mag = ~mag),
          clusterOptions = markerClusterOptions(
          iconCreateFunction=JS("function (cluster)
          var markers = cluster.getAllChildMarkers();
          var sum = 0;
          for (i = 0; i < markers.length; i++)
          sum += Number(markers[i].options.mag);
          // sum += 1;

          return new L.DivIcon( html: '<div><span>' + sum + '</span></div>');
          ")
          )
          )





          share|improve this answer























          • great, thanks much for sharing (+1 +1)! Could you indicate how to keep the default coloring and shape of the clusters? I tried adding back ,className: 'marker-cluster' at the end of the JS function, but it seems we have to re-specify everything

            – Antoine
            Nov 24 '18 at 11:19












          • you have to pass thing like below, two words, seems like. return new L.DivIcon( html: '<div><span>' + sum + '</span></div>', className: 'marker-cluster marker-cluster-medium', iconSize: new L.Point(40,40)); you need your logic to set if marker is large/small/medium, like the original code was doing

            – yosukesabai
            Nov 25 '18 at 4:18











          • By original code I meant class definition of MarkerClusterGroup, _defaultIconCreateFunction(), found in here or somewhere nearby: github.com/Leaflet/Leaflet.markercluster/blob/master/src/…

            – yosukesabai
            Nov 25 '18 at 4:22















          1














          Found my answer. Seems like I can put arbitrary property inside options= in addMarker:



          leaflet(quakes) %>% addTiles() %>% addMarkers(
          options = markerOptions(mag = ~mag),
          clusterOptions = markerClusterOptions(
          iconCreateFunction=JS("function (cluster)
          var markers = cluster.getAllChildMarkers();
          var sum = 0;
          for (i = 0; i < markers.length; i++)
          sum += Number(markers[i].options.mag);
          // sum += 1;

          return new L.DivIcon( html: '<div><span>' + sum + '</span></div>');
          ")
          )
          )





          share|improve this answer























          • great, thanks much for sharing (+1 +1)! Could you indicate how to keep the default coloring and shape of the clusters? I tried adding back ,className: 'marker-cluster' at the end of the JS function, but it seems we have to re-specify everything

            – Antoine
            Nov 24 '18 at 11:19












          • you have to pass thing like below, two words, seems like. return new L.DivIcon( html: '<div><span>' + sum + '</span></div>', className: 'marker-cluster marker-cluster-medium', iconSize: new L.Point(40,40)); you need your logic to set if marker is large/small/medium, like the original code was doing

            – yosukesabai
            Nov 25 '18 at 4:18











          • By original code I meant class definition of MarkerClusterGroup, _defaultIconCreateFunction(), found in here or somewhere nearby: github.com/Leaflet/Leaflet.markercluster/blob/master/src/…

            – yosukesabai
            Nov 25 '18 at 4:22













          1












          1








          1







          Found my answer. Seems like I can put arbitrary property inside options= in addMarker:



          leaflet(quakes) %>% addTiles() %>% addMarkers(
          options = markerOptions(mag = ~mag),
          clusterOptions = markerClusterOptions(
          iconCreateFunction=JS("function (cluster)
          var markers = cluster.getAllChildMarkers();
          var sum = 0;
          for (i = 0; i < markers.length; i++)
          sum += Number(markers[i].options.mag);
          // sum += 1;

          return new L.DivIcon( html: '<div><span>' + sum + '</span></div>');
          ")
          )
          )





          share|improve this answer













          Found my answer. Seems like I can put arbitrary property inside options= in addMarker:



          leaflet(quakes) %>% addTiles() %>% addMarkers(
          options = markerOptions(mag = ~mag),
          clusterOptions = markerClusterOptions(
          iconCreateFunction=JS("function (cluster)
          var markers = cluster.getAllChildMarkers();
          var sum = 0;
          for (i = 0; i < markers.length; i++)
          sum += Number(markers[i].options.mag);
          // sum += 1;

          return new L.DivIcon( html: '<div><span>' + sum + '</span></div>');
          ")
          )
          )






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 '18 at 7:05









          yosukesabaiyosukesabai

          4,28611833




          4,28611833












          • great, thanks much for sharing (+1 +1)! Could you indicate how to keep the default coloring and shape of the clusters? I tried adding back ,className: 'marker-cluster' at the end of the JS function, but it seems we have to re-specify everything

            – Antoine
            Nov 24 '18 at 11:19












          • you have to pass thing like below, two words, seems like. return new L.DivIcon( html: '<div><span>' + sum + '</span></div>', className: 'marker-cluster marker-cluster-medium', iconSize: new L.Point(40,40)); you need your logic to set if marker is large/small/medium, like the original code was doing

            – yosukesabai
            Nov 25 '18 at 4:18











          • By original code I meant class definition of MarkerClusterGroup, _defaultIconCreateFunction(), found in here or somewhere nearby: github.com/Leaflet/Leaflet.markercluster/blob/master/src/…

            – yosukesabai
            Nov 25 '18 at 4:22

















          • great, thanks much for sharing (+1 +1)! Could you indicate how to keep the default coloring and shape of the clusters? I tried adding back ,className: 'marker-cluster' at the end of the JS function, but it seems we have to re-specify everything

            – Antoine
            Nov 24 '18 at 11:19












          • you have to pass thing like below, two words, seems like. return new L.DivIcon( html: '<div><span>' + sum + '</span></div>', className: 'marker-cluster marker-cluster-medium', iconSize: new L.Point(40,40)); you need your logic to set if marker is large/small/medium, like the original code was doing

            – yosukesabai
            Nov 25 '18 at 4:18











          • By original code I meant class definition of MarkerClusterGroup, _defaultIconCreateFunction(), found in here or somewhere nearby: github.com/Leaflet/Leaflet.markercluster/blob/master/src/…

            – yosukesabai
            Nov 25 '18 at 4:22
















          great, thanks much for sharing (+1 +1)! Could you indicate how to keep the default coloring and shape of the clusters? I tried adding back ,className: 'marker-cluster' at the end of the JS function, but it seems we have to re-specify everything

          – Antoine
          Nov 24 '18 at 11:19






          great, thanks much for sharing (+1 +1)! Could you indicate how to keep the default coloring and shape of the clusters? I tried adding back ,className: 'marker-cluster' at the end of the JS function, but it seems we have to re-specify everything

          – Antoine
          Nov 24 '18 at 11:19














          you have to pass thing like below, two words, seems like. return new L.DivIcon( html: '<div><span>' + sum + '</span></div>', className: 'marker-cluster marker-cluster-medium', iconSize: new L.Point(40,40)); you need your logic to set if marker is large/small/medium, like the original code was doing

          – yosukesabai
          Nov 25 '18 at 4:18





          you have to pass thing like below, two words, seems like. return new L.DivIcon( html: '<div><span>' + sum + '</span></div>', className: 'marker-cluster marker-cluster-medium', iconSize: new L.Point(40,40)); you need your logic to set if marker is large/small/medium, like the original code was doing

          – yosukesabai
          Nov 25 '18 at 4:18













          By original code I meant class definition of MarkerClusterGroup, _defaultIconCreateFunction(), found in here or somewhere nearby: github.com/Leaflet/Leaflet.markercluster/blob/master/src/…

          – yosukesabai
          Nov 25 '18 at 4:22





          By original code I meant class definition of MarkerClusterGroup, _defaultIconCreateFunction(), found in here or somewhere nearby: github.com/Leaflet/Leaflet.markercluster/blob/master/src/…

          – yosukesabai
          Nov 25 '18 at 4:22

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245984%2fleaflet-r-how-to-make-appearance-of-clustered-icon-related-to-statistics-of-the%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

          Edmonton

          Crossroads (UK TV series)