闪亮的leaflet多边形点击事件

16
我有一个地图,叫做 business,是从自然地球网站下载的。我创建了一个基本的地图输出,显示这张地图。主要使用了两列数据,即admin(代表国家名称)和economy。然后我在UI中添加了一个下拉列表框Business,当我点击国家的多边形时,该列表将刷新并显示我点击的国家。我假设当我编写 p <- input$Map_shape_click 时,Shiny会知道p是一个business对象,因此它具有admin列,我引用这个admin ID来刷新我的Business下拉列表。但实际上不起作用。链接显示了我所看到的内容 - 当我点击其他国家时,下拉列表不会刷新。

进入图片描述

server.r

country <- readOGR(dsn = tmp, layer = "ne_110m_admin_0_countries", encoding   = "UTF-8")
business<-country[country@data$admin %in% c("Brazil","Colombia","Panama","Kazakhstan","Argentina","India","","Chile","Dominican Republic","United Kingdom","El Salvador","United States of America"),]
business@data$category <- factor(sample.int(20L, nrow(business@data), FALSE))


shinyServer(function(input, output,session) {

output$Map <- renderLeaflet({
  factpal <- colorFactor(topo.colors(20), business@data$category)
  state_popup <- paste0("<strong>Name of the country </strong>", 
                        business$admin, 
                        "<br><strong> information is  </strong>", 
                        business$economy)
    leaflet() %>%
    addProviderTiles("CartoDB.Positron") %>%
    addPolygons(data=business,
                layerId=~admin,
                fillColor= ~factpal(category),
                fillOpacity = 0.7, 
                color = "#BDBDC3", 
                weight = 1, 
                popup = state_popup,
                highlight = highlightOptions(
                weight = 5,
                color = "#666",
                dashArray = "",
                fillOpacity = 0.7,
                bringToFront = TRUE))})


observeEvent(input$Map_shape_click, { # update the location selectInput on map clicks
  p <- input$Map_shape_click
  if(!is.null(p$admin)){
    if(is.null(input$Business) || input$Business!=p$admin) updateSelectInput(session, "Business", selected=p$admin)
  }
})
}
)

ui.r

navbarPage("Market Portal",
tabPanel("About",
           bootstrapPage(
             leafletOutput("Map",width="100%",height="800px"),
             absolutePanel(top=100, right=50,
             selectInput("Business", "Business", c("Brazil","Colombia","Panama","Kazakhstan","Argentina","India","Chile","Dominican Republic","United Kingdom","El Salvador","United States of America"), selected="")
))))

1
map_shape_click 的返回值是坐标 (lat, lng) 和 id(即 layerId 值),因此我认为您想要使用 p$id 而不是 p$admin - SymbolixAU
你是我的英雄!!完美地解决了这个问题!! - user3833612
不客气;我已经把它加为答案了。 - SymbolixAU
1个回答

25
在leaflet中,单击事件返回lat、lng和id(以及一个随机值),因此您只能访问其中之一。
id的值与您在形状绘制函数中指定的layerId相关,因此在您的情况下是layerId = ~admin。
因此,您可以通过点击的id字段访问admin值。
用p$id替换p$admin,就可以解决问题了。
如果您想查看click事件中的内容,请在其周围放置一个print语句。
observeEvent(input$Map_shape_click, { # update the location selectInput on map clicks
  p <- input$Map_shape_click
  print(p)
})

并且它会将该对象打印到控制台。


1
我一直得到空值。 - John Alexander
@JohnAlexander 好的...?你可能应该提出一个新问题。 - SymbolixAU
@JohnAlexander 我敢肯定问题早已不存在了,但可能是您需要调整变量名称。例如,我的映射对象称为“mymap”,而不是“Map”。因此,我需要使用input $ mymap_shape_click,而不是input $ Map_shape_click。 - Leonhard Euler

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接