在我 Shiny 的应用程序中,我想点击我的 mapview map 的一个多边形,然后能够使用 input$map_shape_click
将 layerId 属性提取到一个变量。在下面的代码中,当您单击多边形时,它会打印出 id,但在 mapview 中默认设置为 null。
library(shiny)
library(tmap)
library(leaflet)
library(mapview)
ui <- bootstrapPage(
title = "Standardized Crop Production Index",
tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
mapview::mapviewOutput("map", width = "100%", height = "100%")
)
data("World") #from the tmap library
server <- function(input, output, session) {
out_plot <- observeEvent(input$map_shape_click, {
p <- input$map_shape_click
print(p)
})
output$map <- renderLeaflet({
test <- mapview(World)
test@map
})
}
shinyApp(ui = ui, server = server)
有没有办法设置 mapview 对象的 layerId?我知道如果我只使用 Leaflet 而不是 mapview,我可以使用 addPolygons()
来设置它。但是,我最终想使用通过单击多边形收集的数据作为 mapview 的 popupGraph()
的输入。
如果有一种方法可以从您单击多边形时弹出的表格中检索属性,那就更好了。例如,如果我点击南极洲,会弹出如下属性表:Antarctica Attribute Table .单击多边形时,有什么方法可以检索“名称”属性并将其存储为变量吗?
谢谢!
最佳答案
不完全确定您的第一个问题,因为我不知道可以使用 mapview()
进行分配的任何方式。但是,这是一个使用 addPolygons()
的可重现解决方案:
library(dplyr)
library(shiny)
library(leaflet)
library(leaflet.extras)
library(rgdal)
library(sp)
library(tigris)
library(htmltools)
setwd(dirname(rstudioapi::getActiveDocumentContext()$path)) # set your working directory
philly <- tracts(state = 'PA', county = c('Philadelphia'))
ui <- fluidPage(
title = "Test Map",
leafletOutput("mymap", width = 600)
)
server <- function(input, output, session) {
RV <- reactiveValues(Clicks=list()) # used for storing leaflet variables
tract_labels <- sprintf( # labels for mouseover tooltip
"<strong>%s</strong>, <strong>%s</strong>
<br/><b>Land Area:</b> %s",
philly$COUNTYFP,
philly$STATEFP,
philly$ALAND
) %>% lapply(htmltools::HTML)
output$mymap <- renderLeaflet({ # leaflet map
leaflet(data = philly) %>%
setView(-75.16, 39.9523, zoom = 10) %>%
addTiles(urlTemplate = "https://{s}.tile.openstreetmap.se/hydda/full/{z}/{x}/{y}.png",
attribution = NULL) %>%
addPolygons(data = philly,
layerId = philly@data$ALAND,
group = "regions",
fillColor = "#bdd7e7",
weight = 1,
opacity = 1.0,
fillOpacity = 0.5,
smoothFactor = 0.5,
label = tract_labels,
labelOptions = labelOptions(
style = list("font-weight" = "normal", padding = "3px 8px"),
textsize = "12px",
direction = "auto"),
highlightOptions = highlightOptions(color = "white",
weight = 2,
bringToFront = TRUE))
})
observeEvent({input$mymap_shape_click}, {
#create object for clicked polygon
click <- input$mymap_shape_click
RV$Clicks <- c(RV$Clicks,click$id)
#define leaflet proxy for second regional level map
proxy <- leafletProxy("mymap")
#subset regions shapefile by the clicked on polygons
selectedReg <- philly[philly@data$ALAND == click$id,]
#map clicked on polygons
proxy %>% addPolygons(data = selectedReg,
fillColor = "red",
fillOpacity = 1,
weight = 1,
color = "black",
stroke = T,
group = "selected",
layerId = selectedReg@data$ALAND)
# remove polygon group that are clicked twice
if(click$group == "selected"){
proxy %>%
clearGroup(group = "selected")
RV$Clicks <- 0 # resets values if polygons are clicked twice
}
mean.land <- mean(as.numeric(RV$Clicks)) # stores the values of polygons that are clicked
print(mean.land)
})
}
shinyApp(ui, server)
本质上, map 有两个层:基本区域层和另一个突出显示您单击内容的区域多边形。您可以单击每个多边形以从每个多边形中“检索”一个值(在本例中为土地面积或变量 ALAND)并对其进行计算。在这里,我选择了三个多边形,并使用了 mean.land
变量来显示这三个多边形的平均土地面积。
reactiveValues RV
对象用于存储您单击的任何多边形上的 layerId
变量的数值。这允许您存储和“检索”它以用于您可能想要进行的其他计算。
[1] 717210 # first click, first value
[1] 571940 # second click, averaged value
[1] 488678.3 # third click, averaged value
您可以通过更改代码中对变量 ALAND 的任何引用来更改提取 layerId
属性。
关于r - 如何使用mapview设置Leaflet layerId,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52436094/