我用https://data.london.gov.uk/dataset/statistical-gis-boundary-files-london和闪亮的R和传单制作了伦敦地图。我在shapefile中添加了一个属性,现在希望能够在用户单击特定多边形时突出显示shapefile并打印信息。
我查看了shiny leaflet ploygon click event,Marker mouse click event in R leaflet for shiny和How to implement input$map_marker_click correctly?,并知道我需要使用ObserveEvent
,但无法正确实现。
我的代码是:
library(shiny)
library("rgdal")
library(leaflet)
shapeData <- readOGR('statistical-gis-boundaries-london/ESRI/LSOA_2004_London_Low_Resolution.shp')
shapeData <- spTransform(shapeData, CRS("+proj=longlat +ellps=GRS80"))
shapeData$col=sample(c('red','yellow','green'),nrow(shapeData),1) #add some value you want to map
borough=read.csv('BoroughCentres.csv')
ui=fluidPage(
fluidPage(
leafletOutput('LSOAMap'),
p(),
selectInput('LANAME','Borough',
choices = unique(shapeData$LA_NAME))
)
)
server=function(input, output) {
output$LSOAMap <- renderLeaflet({
llong=borough[borough$Borough==input$LANAME,3]
llat=borough[borough$Borough==input$LANAME,4]
bor=subset(shapeData,shapeData$LA_NAME %in% input$LANAME)
leaflet() %>% addTiles() %>%
setView(lng = llong, lat=llat,zoom=13) %>%
addPolygons(data=bor,weight=2,col = 'black',fillOpacity = 0.2,fillColor = bor$col,
highlightOptions = highlightOptions(color='white',weight=1,
bringToFront = TRUE)) %>%
addMarkers(lng = llong,lat=llat,popup=input$LANAME)
})
}
shinyApp(ui, server)
我尝试添加session
作为参数:
observe({
click <- input$map_marker_click
if (is.null(click))
return()
print(click)
text <-
paste("Lattitude ",
click$lat,
"Longtitude ",
click$lng)
leafletProxy(mapId = "LSOAMap") %>%
clearPopups() %>%
addPopups(dat = click, lat = ~lat, lng = ~lng, popup = text)
# map$clearPopups()
# map$showPopup(click$latitude, click$longtitude, text)
})
无济于事。
我想要的是,当用户突出显示特定形状时,文本会弹出并显示shapefile中相应的STWARDNAME
。
自治市镇的前几行是:
> head(borough)
Borough LA_CODE long lat
1 City of London E09000001 -0.09194991 51.51814
2 Barking and Dagenham E09000002 0.13064556 51.54764
3 Barnet E09000003 -0.20416711 51.61086
4 Bexley E09000004 0.13459320 51.45981
5 Brent E09000005 -0.26187070 51.55697
6 Bromley E09000006 0.03734663 51.38836
答案 0 :(得分:1)
您已经很接近了,但是有一些语法问题,我认为这会绊倒您。
首先,您没有从SPDF中正确选择所需的public interface A {
/**
* Write your method java doc
* @param a The int must be in milliseconds
*/
void doSth(int a);
}
,因此R无法知道要显示哪个。我添加了这一行
int a
此外,您应该将int milliseconds
传递给STWARDNAME
最后,由于您对多边形的鼠标悬停更加感兴趣,因此我删除了弹出标记,并更改为popup = subset(shapeData, LA_NAME == input$LANAME )$STWARDNAME
,以便在将鼠标悬停在多边形上时显示名称:
highlightOptions
答案 1 :(得分:0)
@Stedy的回答很棒,而且更好的是,我采用了另一种方式。我添加了一个图层ID向量,并弹出了查找STWARDNAME的决定
lid=as.vector(row.names(bor@data))
pu=bor@data[row.names(bor@data)==lid,'STWARDNAME']
,然后在制作地图layerId = lid,popup = pu,
时添加了两个参数。当用户单击区域时显示弹出窗口
bor=subset(shapeData,shapeData$LA_NAME %in% input$LANAME)
lid=as.vector(row.names(bor@data))
pu=bor@data[row.names(bor@data)==lid,'STWARDNAME']
leaflet() %>% addTiles() %>%
setView(lng = llong, lat=llat,zoom=13) %>%
addPolygons(data=bor,weight=2,col = 'black',fillOpacity = 0.2,fillColor = bor$col,
layerId = lid,popup = pu,
highlightOptions = highlightOptions(color='white',weight=1,
bringToFront = TRUE)) %>%
addMarkers(lng = llong,lat=llat,popup=input$LANAME)