shape_click$id 为 NULL

问题描述 投票:0回答:1

基于here的这个示例,我想通过简单地单击感兴趣的区域来将地图与 Highchart 动态链接。然而,shape_click 函数返回 $id NULL。我用不同的 shapefile 尝试过,但不起作用。 您将在下面看到调整后的示例中的代码(可在此处获取数据,以从 shape_click 函数获取 $id。

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)
r shiny r-leaflet
1个回答
6
投票

您必须在

layerId
通话中添加
addPolygons

只需将最后一个

observe
更改为(如果PRUID代表ID列):

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)
  })
© www.soinside.com 2019 - 2024. All rights reserved.