闪亮的传单ploygon点击事件

use*_*612 11 r leaflet shiny

我有一张地图叫做business从天然地球网站下载.我在这里做的是创建了一个显示地图的基本Map输出.我主要使用两列admin(这是国家的名称)和economy这里.然后我添加了一个Business在ui 下调用的下拉列表,这样当我单击该国家/地区的多边形时,列表将刷新并显示我要点击的国家/地区.我假设当我写p <- input$Map_shape_click闪亮时会知道p是一个business对象,所以它有列admin,我已经引用此adminID来刷新我的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)
  }
})
}
)
Run Code Online (Sandbox Code Playgroud)

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="")
))))
Run Code Online (Sandbox Code Playgroud)

Sym*_*xAU 14

click在单张收益事件lat,lng并且id(和一个随机值).所以你只能访问其中一个元素.

id值与layerId您在形状绘图函数中指定的值相关,因此在您的情况下是layerId=~admin.

所以你admin通过点击的id字段来控制价值

替换p$adminp$id,你应该有你的解决方案.


如果你想看看click活动中的内容,只需print在其周围加上一个声明即可

observeEvent(input$Map_shape_click, { # update the location selectInput on map clicks
  p <- input$Map_shape_click
  print(p)
})
Run Code Online (Sandbox Code Playgroud)

它会将对象打印到控制台.