我想通过使用串扰而不是 shiny 来链接传单图和 DT 库创建的数据表。因此,当我单击表侧的任何记录时,地图中的圆圈将突出显示。我知道生成传单地图需要纬度和长度,但是有没有办法让表边只有名称和面积列(不显示纬度和长度(?
这是我的示例代码:
library(leaflet)
library(DT)
library(crosstalk)
df <- read.csv(textConnection(
"Name,Lat,Long, area
Samurai Noodle,47.597131,-122.327298,40
Kukai Ramen,47.6154,-122.327157,30
Tsukushinbo,47.59987,-122.326726,10"
))
df$Name <- as.character(df$Name)
sdf <- SharedData$new(df, ~df$Name)
pal <- colorNumeric("RdYlBu", df$area)
labels <- paste(sep = "<br/>",
paste('Name: ', df$Name),
paste('Area: ', df$area))
d1 <- leaflet(sdf) %>%
addTiles() %>%
addCircleMarkers(~Long,
~Lat,
radius = df$area,
color = ~pal(df$area),
fillColor = ~pal(df$area),
popup = labels,
fillOpacity = 1) %>%
addLegend("topright",
title = "AREA",
pal = pal,
values = df$area,
opacity = 1)
d2 <- datatable(sdf, width = "100%")
bscols(d1, d2)
你不需要另一个共享数据。
数据表的选项中有一个快速的解决方案。
datatable(sdf, options=list(columnDefs = list(list(visible=FALSE,
targets=c(2,3))))) #positions
我今天想通了这个问题。
我需要做的是创建df_2(删除日志和纬度(,并使用df_2创建另一个 SharedData 对象,并添加组。
sdf_2 <- SharedData$new(df_2, ~df$Name, group = "data_subset")
使用 sdf 和 sdf_2 分别创建传单地图和数据表:
d1 <- leaflet(sdf) %>% addTiles() %>% addCircleMarkers(~Long,
~Lat,
radius = df$area,
color = ~pal(df$area),
fillColor = ~pal(df$area),
popup = labels,
fillOpacity = 1) %>% addLegend("topright",
title = "AREA",
pal = pal,
values = df$area,
opacity = 1)
d2 <- datatable(sdf_2, width = "100%")
bscols(d1, d2)