您如何遍历 SharedData 对象以便将单独的 addPolylines 绘制到 R 中的 Leaflet 地图?
我有具有不同“路线”的数据集,我在循环中将它们一一添加for
,以免它们相互连接。接下来,我希望它们都能响应Crosstalk
包添加的滑块,但我在循环SharedData$new
对象时遇到问题。
我努力了:
#Load libraries
library(dplyr); library(leaflet)
# An example of my data:
data <- data.frame(lon = c(12.42111, 12.42111, 12.41170, 12.42111, 12.39508, 12.39970, 12.42111, 12.39508),
lat = c(55.73615, 55.73615, 55.74155, 55.73615, 55.73074, 55.73871, 55.73615, 55.73074),
date = c("2020-03-28 12:46:29", "2020-03-28 13:20:42", "2020-03-28 13:50:47", "2020-03-28 18:33:44",
"2020-03-28 19:24:11", "2020-03-28 20:31:29", "2020-03-28 21:33:29", "2020-03-28 21:42:05"),
group = c(1, 1, 1, 1, 2, 2, 2, 2))
data$date <- as.POSIXct(data$date, format="%Y-%m-%d %H:%M:%S")
# Create leaflet map
temp_leaflet <- leaflet(data) %>% addTiles()
# for loop way of adding grouped routes
for (i in unique(factor(data$group))) {
temp_leaflet <- temp_leaflet %>%
addPolylines(data = data[data$group == i, ],
lng = ~lon, lat = ~lat, group = i)
}
temp_leaflet <- temp_leaflet %>%
hideGroup(unique(factor(data$group))) %>%
addLayersControl(
overlayGroups = unique(factor(data$group)),
options = layersControlOptions(collapsed = F))
所以这就是我这样做的方式,但是如果我希望能够在date
带有包的滑块(列)中按时间更改两条路线Crosstalk
,我根本无法弄清楚如何遍历一个SharedData$new
对象并将其分组,使其默认隐藏。
关于如何实现这一目标的任何想法?