我有一张名为business
的地图,该地图是从自然地球网站下载的。我在这里做的是创建了一个显示地图的基本Map输出。我主要使用两列admin
(这是国家的名称)和economy
。然后我在ui下添加了一个名为Business
的下拉列表,这样当我单击该国家/地区的多边形时,列表将刷新并显示我要点击的国家/地区。我假设当我写p <- input$Map_shape_click
闪亮时会知道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="")
))))
答案 0 :(得分:14)
传单中的click
事件返回lat
,lng
和id
(以及随机值)。所以你只能访问其中一个元素。
id
值与您在形状绘图功能中指定的layerId
相关,因此在您的情况下为layerId=~admin
。
因此,您可以通过点击的admin
字段
id
值
将p$admin
替换为p$id
,您应该有自己的解决方案。
如果您想查看click
事件中的内容,只需在其周围添加print
语句
observeEvent(input$Map_shape_click, { # update the location selectInput on map clicks
p <- input$Map_shape_click
print(p)
})
它会将对象打印到控制台。