在有光泽/传单中选择和取消选择折线 [英] Select and Deselect Polylines in Shiny/Leaflet
问题描述
我解决了劳伦的问题. 使用Leaflet选择和取消选择多个多边形时更改样式/闪亮 唯一的区别是我使用折线而不是多边形.我想选择多条折线,然后再次单击以取消选择它们.但这不起作用.它从表中删除了重新选择的内容,但从地图中删除了该内容,并且从我选择的行中删除了一条线后,我再也无法选择它.
I solved my problem as Lauren. Changing styles when selecting and deselecting multiple polygons with Leaflet/Shiny The only difference is that I use polylines instead of polygons. I want to select multiple polylines und deselect them at click again. But it doesn't work..it deletes the reselected from the table but not from the map and after a line was deleted from my selected lines I can't select it anymore.
有人可以帮我吗!
这是我的代码:
library(shiny)
library(leaflet)
library(geojsonio)
url <- "pathTogeojson"
geojson <- geojsonio::geojson_read(url, what = "sp")
shinyApp(
ui <- fluidRow(
leafletOutput("map")),
server <- function(input, output, session) {
click_list <- reactiveValues(ids = vector())
output$map <- renderLeaflet({
leaflet() %>%
addTiles() %>%
setView(lng=16.357795000076294, lat=48.194883921677935, zoom = 15) %>%
addPolylines(data=geojson, layerId = geojson@data$name_1, group = "selected", color="red", weight=3,opacity=1)
})
observeEvent(input$map_shape_click, {
click <- input$map_shape_click
proxy <- leafletProxy("map")
click_list$ids <- c(click_list$ids, click$id)
sel_lines <- geojson[geojson@data$name_1 %in% click_list$ids, ]
if(click$id %in% sel_lines@data$id)
{
nameMatch <- sel_lines@data$name_1[sel_lines@data$id == click$id]
click_list$ids <- click_list$ids[!click_list$ids %in% click$id]
click_list$ids <- click_list$ids[!click_list$ids %in% nameMatch]
proxy %>% removeShape(layerId = click$id)
}
else
{
proxy %>% addPolylines(data = sel_lines, layerId = sel_lines@data$id, color="#6cb5bc", weight=5,opacity=1)
}
})
})
推荐答案
我自己找到了解决方案.我的数据和我的不理解是问题所在.它仅在所有使用的列均为character
类型时有效...所以我不得不使用as.character()
I found the solution by my own..my data and my incomprehension were the problem. It only works, when all used columns are type character
...so i had to do a type conversion with as.character()
这篇关于在有光泽/传单中选择和取消选择折线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!