問題描述
我試圖在 R 傳單中找到一種方法來包含覆蓋按鈕,以過濾掉數據中的組.我還需要包含用于切換數據中表示的列的單選按鈕.我似乎找不到使用 addLayersControl() 函數在 R 傳單中執行此操作的方法.
I am trying to find a way in R leaflet to include overlay buttons which filters out groups in the data. I also need to include radio buttons which switch the column which is being represented in the data. I can't seem to find a way to do this in R leaflet using the addLayersControl() function.
我最初認為可以將多個組添加到單個圖層并使用 baseGroups 和 overlayGroups(如下面的代碼所示).然而,這并沒有達到預期的結果.如果有人可以提出另一種方法來實現這一目標,我將不勝感激.最好不要發亮.
I initial thought it would be possible to add multiple groups to a single layer and use baseGroups and overlayGroups (as seen in the code below). However, this does not achieve the desired results. I would appreciate it if someone could suggest an alternative way to achieve this. Preferably without shiny.
library(dplyr)
library(leaflet)
data <- data.frame(Name = c("A", "A", "A", "B", "B", "C", "C", "C"),
Value1 = c(12,43,54,34,23,77,44,22),
Value2 = c(6,5,2,7,5,6,4,3),
Lat = c(51.1, 51.6, 57.3, 52.4, 56.3, 54.3, 60.4, 49.2),
Lon = c(5, -3, -2, -1, 4, 3, -5, 0))
data %>%
leaflet() %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addCircles(lat=~Lat, lng=~Lon, radius = ~Value1*1000, group=c(~Name, "Value1")) %>%
addCircles(lat=~Lat, lng=~Lon, radius = ~Value2, group=c(~Name, "Value2")) %>%
addLayersControl(
baseGroups = c("Value1", "Value2"),
overlayGroups = c("A", "B", "C"),
options = layersControlOptions(collapsed = F)
)
圖像:輸出不是我所期望的
推薦答案
下面是一個非常不優雅和 hacky 的問題解決方案.它為每個圓圈分配一個圖層 ID,并使用一些 javascript 來確定在給定輸入復選框的情況下應該顯示哪個圓圈.
Below is a very inelegant and hacky solution to the problem. It assigns a layer id to each circle and uses some javascript to determine which circle should be displayed given the input checkboxes.
可以在此處找到工作演示:https://rpubs.com/Jumble/leaflet_layer_control
A working demonstration can be found here: https://rpubs.com/Jumble/leaflet_layer_control
如果有人有更優雅的解決方案,請分享.
Please share if anyone has a more elegant solution.
library(dplyr)
library(leaflet)
library(htmlwidgets)
data <- data.frame(ID = c("1", "2","3","4","5","6","7","8"),
Name = c("A", "A", "A", "B", "B", "C", "C", "C"),
Value1 = c(12,43,54,34,23,77,44,22),
Value2 = c(6,5,2,7,5,6,4,3),
Lat = c(51.1, 51.6, 57.3, 52.4, 56.3, 54.3, 60.4, 49.2),
Lon = c(5, -3, -2, -1, 4, 3, -5, 0))
data %>%
leaflet() %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addCircles(lat=~Lat, lng=~Lon, radius = ~Value1*1000, group=~Name, label=~Name, popup=~as.character(Value1), layerId = ~paste(ID,"Value1", sep="")) %>%
addCircles(lat=~Lat, lng=~Lon, radius = ~Value2, group=~Name, label=~Name, popup=~as.character(Value2), layerId = ~paste(ID,"Value2", sep="")) %>%
addLayersControl(
baseGroups = c("Value1", "Value2"),
overlayGroups = c("A", "B", "C"),
options = layersControlOptions(collapsed = F)
) %>%
htmlwidgets::onRender("
function(el, x) {
var myMap = this;
var baseLayer = 'Value1';
myMap.eachLayer(function(layer){
var id = layer.options.layerId;
if (id){
if ('Value1' !== id.substring(1,)){
layer.getElement().style.display = 'none';
}
}
})
console.log(myMap.baselayer);
myMap.on('baselayerchange',
function (e) {
baseLayer=e.name;
myMap.eachLayer(function (layer) {
var id = layer.options.layerId;
if (id){
if (e.name !== id.substring(1,)){
layer.getElement().style.display = 'none';
layer.closePopup();
}
if (e.name === id.substring(1,)){
layer.getElement().style.display = 'block';
}
}
});
})
myMap.on('overlayadd', function(e){
myMap.eachLayer(function(layer){
var id = layer.options.layerId;
if (id){
if (baseLayer !== id.substring(1,)){
layer.getElement().style.display = 'none';
}
}
})
})
}")
這篇關于R Leaflet:將多個組分配給一個層以過濾數據并更改表示的列的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!