R 传单 Shiny : shape_click$id is NULL

标签 r shiny click leaflet shapes

基于这个例子here ,我想通过简单地点击感兴趣的区域来动态链接 map 和 Highchart。但是 shape_click 函数返回 $id NULL。我用不同的 shapefile 试过它,但它不起作用。 您将在下面看到调整后示例中的代码(可用数据 here 从 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)

谢谢

最佳答案

您必须在 addPolygons 调用中添加一个 layerId

只需将最后一个 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)
  })

关于R 传单 Shiny : shape_click$id is NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51375265/

相关文章:

r - 如何在gganimate中显示y轴的当前值?

r - 在 R 中将空值转换为 NULL - 处理日期列

r - 仅第一个多边形出现在 Leaflet 2.0.1 等值线图中

r - 如何防止 Shiny 的应用程序变灰?

带缩略图的 jQuery 画廊,单击功能只能运行一次

jquery - 动态生成的 <li> 中未触发单击事件

r - 检查一个字符串的所有字符是否存在于 r 中的另一个字符串中

r - 计算日期范围内有重叠和间隙的唯一天数

r - 重新加载时 Shiny 的reactiveUI重置值

jquery - 保留:hover color on mouse click via Jquery