我可以根据某些字符的存在在 Leaflet(在 Rstudio 中)中显示某些标记吗?

Can I display certain markers in Leaflet (in Rstudio) based on the presence of certain characters?

提问人:MMamone 提问时间:11/18/2023 更新时间:11/18/2023 访问量:15

问:

我有一张传单图,上面有基于不同物种存在/不存在数据的标记。我希望能够有一个物种菜单可供选择,并且地图将仅显示存在该物种的地点的标记(例如,选择物种 B,2 和 4 的标记将被隐藏)。有没有办法在不为每个站点创建单独的图层的情况下实现此目的?我的实际数据中大约有 100 个站点。

df<-data.frame(sites,longitude,latitude,speciesA,speciesB,speciesC)
site_name<-function(k){
  df[df$sites==k, ]
}
species_present<-function(h){
  subset(h, select = speciesA:speciesC) |>
    sapply(function(z) all(z == 1)) |> which() |> names()
}
marker<-function(b){
  species_present(site_name(b))
}
marker_list<-lapply(as.list(df$sites),marker)
class(marker_list)<-"character"
new_df<-df[ ,c("sites","longitude","latitude")]
df_3<-cbind(new_df,marker_list)
leaflet()%>%
  addTiles()%>%
  setView(lng=-78,lat=44,zoom=5)%>%
  addMarkers(df_3,lng=df_3$longitude,lat=df_3$latitude,popup = paste(
    df_3$sites
    ,"<br>"
    ,df_3$marker_list))```

I have explored the solution laid out here https://stackoverflow.com/a/48063343/22801263, but I'm not sure how I could apply it to my map given that I'm looking to filter out data based on the presence of a character rather than the value of a continuous variable.  
My eventual goal is to make an app using rShiny, so if there is a solution within Shiny itself I am very much open to that as well.  The presence/absence data for the species in question will be updated annually, so I am hoping to have a map that can be updated simply by changing the species in the matrix without heavily editing the code for the map itself each time.
r

评论


答: 暂无答案