R传单闪亮:shape\u click$id为空
基于这个示例,我希望通过单击感兴趣的区域,将地图与Highchart动态链接。但是,shape\u click函数返回$id NULL。我用不同的shapefile试过了,但不起作用。 您将看到下面示例中的代码调整(数据可用,可从shape_click函数获取$id)R传单闪亮:shape\u click$id为空,r,shiny,click,leaflet,shape,R,Shiny,Click,Leaflet,Shape,基于这个示例,我希望通过单击感兴趣的区域,将地图与Highchart动态链接。但是,shape\u click函数返回$id NULL。我用不同的shapefile试过了,但不起作用。 您将看到下面示例中的代码调整(数据可用,可从shape_click函数获取$id) library(shiny) library(dplyr) library(leaflet) library(sf) library(rmapshaper) ui <- fluidPage( titlePanel("
library(shiny)
library(dplyr)
library(leaflet)
library(sf)
library(rmapshaper)
ui <- fluidPage(
titlePanel("heatmap"),
# Sidebar with a slider input for year of interest
sidebarLayout(
sidebarPanel(
sliderInput("year",h3("Select year or push play button"),
min = 2000, max = 2002, step = 1, value = 2000,
animate = TRUE)
),
# Output of the map
mainPanel(
leafletOutput("unemployment"),
verbatimTextOutput('summary')
)
)
)
server <- function(input, output) {
#to get the spacial data: from file in link above
data.p <- sf::st_read("input/gpr_000a11a_e.shp") %>%
st_transform(4326) %>%
rmapshaper::ms_simplify()
data.p$PRUID <- as.character(data.p$PRUID) %>% as.numeric
data.p <- data.p[which(data.p$PRUID < 60),]
lng.center <- -99
lat.center <- 60
zoom.def <- 3
#dataframe with same structure as statscan csv after processing
unem <- runif(10,min=0,max=100)
unem1 <- unem+runif(1,-10,10)
unem2 <- unem1+runif(1,-10,10)
unemployment <- c(unem,unem1,unem2)
#dataframe with same structure as statscan csv after processing
X <- data.frame("id" = c(10,11,12,13,24,35,46,47,48,59,
10,11,12,13,24,35,46,47,48,59,
10,11,12,13,24,35,46,47,48,59),
"Unemployment" = unemployment,
"year" = c(rep(2000,10),rep(2001,10),rep(2002,10))
)
data <- left_join(data.p, X, by = c("PRUID"= "id"))
output$unemployment <- renderLeaflet({
leaflet(data = data.p) %>%
addProviderTiles("OpenStreetMap.Mapnik", options = providerTileOptions(opacity = 1), group = "Open Street Map") %>%
setView(lng = lng.center, lat = lat.center, zoom = zoom.def) %>%
addPolygons(group = 'base',
fillColor = 'transparent',
color = 'black',
weight = 1.5) %>%
addLegend(pal = pal(), values = X$Unemployment, opacity = 0.7, title = NULL,
position = "topright")
})
get_data <- reactive({
data[which(data$year == input$year),]
})
pal <- reactive({
colorNumeric("viridis", domain = X$Unemployment)
})
observe({
data <- get_data()
leafletProxy('unemployment', data = data) %>%
clearGroup('polygons') %>%
addPolygons(group = 'polygons',
fillColor = ~pal()(Unemployment),
fillOpacity = 0.9,
color = 'black',
weight = 1.5)
})
output$summary <- renderPrint({
event <- input$unemployment_shape_click
print(event)
})
}
# Run the application
shinyApp(ui = ui, server = server)
库(闪亮)
图书馆(dplyr)
图书馆(单张)
图书馆(sf)
图书馆(rmapshaper)
ui您必须在addPolygons
调用中添加一个layerId
只需将最后一个observe
更改为(如果PRUID表示ID列):
观察({
数据%
clearGroup(‘多边形’)%%>%
添加多边形(组='多边形',
fillColor=~pal()(失业),
fillOpacity=0.9,layerId=data$PRUID,
颜色='黑色',
重量=1.5)
})
您必须在addPolygons
调用中添加一个layerId
只需将最后一个observe
更改为(如果PRUID表示ID列):
观察({
数据%
clearGroup(‘多边形’)%%>%
添加多边形(组='多边形',
fillColor=~pal()(失业),
fillOpacity=0.9,layerId=data$PRUID,
颜色='黑色',
重量=1.5)
})
observe({
data <- get_data()
leafletProxy('unemployment', data = data) %>%
clearGroup('polygons') %>%
addPolygons(group = 'polygons',
fillColor = ~pal()(Unemployment),
fillOpacity = 0.9, layerId = data$PRUID,
color = 'black',
weight = 1.5)
})