传单R,如何使与孩子统计相关的聚类图标的外观? [英] leaflet R, how to make appearance of clustered icon related to statistics of the children?
问题描述
我想基于子标记属性的总和在Leaflet/Shiny应用程序中自定义聚簇标记的外观.
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?
使用纯JavaScript应用程序,似乎我应该能够将自定义属性设置为单个标记,然后像在
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.
但是我正在从R的传单中添加带有addCircleMarkers
和addMarkers
的标记,而且似乎无法将任意属性添加到正在生成的标记中.下面的代码有效,但是如果我取消注释两行(mag = ~mag
和sum += markers[i].mag;
)
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>'});
}")
)
)
我考虑过使用addMarkers
的label=
选项,然后从Javascript解析它.但是用getAllChildMarkers()
访问的JS标记群集上的标记似乎没有label
属性.
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.
我还考虑过以某种方式将数据帧从R传递到Leaflet(JS),也许此. ..?
I also thought about passing a dataframe from R to leaflet(JS), somehow, maybe like this example, or this ...?
推荐答案
找到了我的答案.好像我可以在addMarker
中的options=
中放置任意属性:
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>'});
}")
)
)
这篇关于传单R,如何使与孩子统计相关的聚类图标的外观?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!