我有一张地图叫做business
从天然地球网站下载.我在这里做的是创建了一个显示地图的基本Map输出.我主要使用两列admin
(这是国家的名称)和economy
这里.然后我添加了一个Business
在ui 下调用的下拉列表,这样当我单击该国家/地区的多边形时,列表将刷新并显示我要点击的国家/地区.我假设当我写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)
}
})
}
)
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$admin
为p$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)
它会将对象打印到控制台.