串扰:过滤传单中的折线 [英] Crosstalk: filter Polylines in Leaflet
问题描述
我无法使用传单和折线进行串扰 - 这是一个 MWE:
I can't get crosstalk to work with leaflet and Polylines - here is an MWE:
library(crosstalk)
library(leaflet)
theta <- seq(0, 2*pi, len = 100)
dat <- data.frame(
lon = cos(theta),
lat = sin(theta),
t = 1:100
)
sd <- SharedData$new(dat)
map <- leaflet() %>%
addTiles() %>%
addCircleMarkers(data = sd, lat = ~lat, lng = ~lon, color = "blue") %>%
addPolylines(data = sd, lat = ~lat, lng = ~lon, color = "blue")
bscols(
filter_slider("t", "Time", sd, column = ~t),
map
)
时间 filter_slider 适用于圆形标记,但不适用于折线.
The time filter_slider applies to the circle markers but not the polylines.
如果有人能指出我正确的方向,很高兴在 R 传单包中解决这个问题.IE.需要改变/实施什么?我认为到目前为止 javascript 方面缺少支持?
Happy to having a go at fixing this in the R leaflet package if someone can point me in the right direction. I.e. what would be required to change / implement? I assume the support is missing on the javascript side as of now?
推荐答案
更新:好消息!@dmurdoch 已提交拉取请求以添加对折线和多边形的支持.使用他的串扰版本,您现在可以过滤传单线/多边形,如果它们是 sp 对象(注意,它似乎还不适用于 sf).
UPDATE: Good News! @dmurdoch has submitted a pull request to add support for polylines and polygons. Using his version of crosstalk, you can now filter leaflet lines/polygons if they're sp objects (note, it doesn't seem to work with sf yet).
首先你需要安装这个版本的串扰:devtools::install_github("dmurdoch/leaflet@crosstalk4")
First you will need to install this version of crosstalk:
devtools::install_github("dmurdoch/leaflet@crosstalk4")
然后你需要确保你的特征是空间对象,使用 rgdal 或光栅很容易:shapes_to_filter <- raster::shapefile("data/features.shp") # 栅格导入到空间对象"shape_to_filter <- rgdal::readOGR("data/features.shp") # rgdal 导入到空间对象"
Then you will need to make sure your features are Spatial objects, easy using rgdal or raster:
shapes_to_filter <- raster::shapefile("data/features.shp") # raster import to 'Spatial Object'
shapes_to_filter <- rgdal::readOGR("data/features.shp") # rgdal import to 'Spatial Object'
或者,如果您将 sf 和 dplyr 用于大多数空间任务(如我),请将 sf 对象转换为空间:
Or, if you use sf and dplyr for most spatial tasks (like me) convert an sf object to Spatial:
库(dplyr)图书馆(SF)shape_to_filter <- st_read("data/features.shp") %>% as('Spatial') # sf 导入到'空间对象'
然后为传单创建一个 sd 对象,并为过滤器创建一个数据框副本(重要:注意如何使用 sd_map 中的组名设置 sd_df 的组):
Then create an sd object for leaflet, and a data frame copy for the filters (IMPORTANT: note how the group for sd_df is set using the group names from the sd_map) :
库(串扰)sd_map <- SharedData$new(shapes_to_filter)sd_df <- SharedData$new(as.data.frame(shapes_to_filter@data), group = sd_map $groupName())
使用 sd_df 创建串扰滤波器:
Create crosstalk filters using sd_df:
filter_select("filterid", "选择过滤器标签", sd_df, ~SomeColumn)
使用 sd_map 对象创建地图:
Create the map using the sd_map object:
图书馆(传单)传单()%>%addProviderTiles("OpenStreetMap") %>%addPolygons(data = sd_map)
并且任何链接的表/图表也需要使用 sd_df 对象:
And any linked tables/charts need to also use the sd_df object:
库(DT)数据表(sd_df)
以下是解决方案的所有来源:
Here's all of the sources for the solution:
来自 dmurdoch 的 Github 拉取请求以添加对多边形/线的支持
原始解决方案 - 使用过时的方法sd>$transform"
Original solution - with outdated method "sd$transform"
更新示例 - 使用新的组"方法,但我无法让他们的 RMD 工作一个>
这篇关于串扰:过滤传单中的折线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!