在我闪亮的应用程序中,我想单击mapview地图的多边形,并能够使用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()
的输入。
如果有一种方法可以从单击多边形时弹出的表中检索属性,那就更好了。例如,如果我单击“南极洲”,则会弹出以下属性表:https://github.com/numpy/numpy/blob/v1.15.1/numpy/lib/npyio.py#L384。单击多边形后,是否可以检索“名称”属性并将其存储为变量?
谢谢!
答案 0 :(得分:1)
不确定您的第一个问题,因为我不知道可以使用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)
基本上,地图包含两层:基本区域图层和另一个区域多边形,以突出显示您单击的内容。您可以单击每个多边形以从每个多边形中“检索”一个值(在这种情况下为陆地面积,或变量ALAND)并对其进行计算。在这里,我选择了三个多边形,并使用了mean.land
变量来显示所有三个多边形的平均陆地面积。
reactorValues RV
对象用于在您单击的任何多边形上存储layerId
变量的数值。这使您可以存储和“检索”它,以进行其他可能要进行的计算。
[1] 717210 # first click, first value
[1] 571940 # second click, averaged value
[1] 488678.3 # third click, averaged value
您可以通过更改代码中对变量ALAND的任何引用来更改提取layerId
属性。
答案 1 :(得分:1)
有点晚了,但是可以在地图视图中设置login-config
。 mapview将通过layerId
参数(包括...
)将任何其他(有效)参数传递给相应的传单功能。唯一的区别是您不能将公式符号与mapview一起使用,因为它只会将几何图形部分传递给layerId
(在这种情况下)。因此,在评估公式时,没有任何数据附加到几何图形,因此失败了。不过,您可以做的是将相应的向量传递到addPolygons
。
总结起来,以下代码应返回layerId
上的layerId
:
input$map_shape_click