闪亮的传单 ploygon 点击事件 [英] shiny leaflet ploygon click event
问题描述
我有一张名为 business
的地图,它是从 natural earth 网站下载的.我在这里所做的是创建了一个显示地图的基本地图输出.我在这里主要使用 admin
(国家名称)和 economy
两列.然后我在 ui 下添加了一个名为 Business
的下拉列表,这样当我单击国家的多边形时,列表会刷新并显示我正在单击的国家.我假设当我写 p <- input$Map_shape_click
闪亮会知道 p 是一个 business
对象所以它有列 admin
并且我有参考这个 admin
ID 刷新我的 Business
下拉列表.但这不起作用.该链接显示了我所看到的内容 - 当我单击其他国家/地区时,列表不会刷新.
I have a map called business
which is downloaded from natural earth website. What I am doing here is I created a basic Map output which shows the map. I am mainly using two columns which are admin
(which is the name of the country) and economy
here. Then I added a drop down list called Business
under ui so that when I click the polygon of the country the list would refresh and show the country that I am clicking. I assume when I write p <- input$Map_shape_click
shiny would know p is a business
object so it has column admin
and I have reference this admin
ID to refresh my Business
drop down list. But it doesn't work.The link shows what I am seeing - the list wouldn't refresh when I click a different country.
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="")
))))
推荐答案
leaflet中的click
事件返回lat
、lng
和id
(和一个随机值).因此,您只能访问其中一个元素.
The click
event in leaflet returns lat
, lng
and id
(and a random value). So you can only access one of those elements.
id
值与您在形状绘图函数中指定的 layerId
相关,因此在您的情况下为 layerId=~admin
.
The id
value relates to the layerId
you specify in the shape plotting function, so in your case that's layerId=~admin
.
所以你通过点击的id
字段访问admin
值
So you acccess the admin
value through the click's id
field
将 p$admin
替换为 p$id
,您应该有自己的解决方案.
replace p$admin
with p$id
and you should have your solution.
如果您想查看 click
事件中的内容,只需在其周围添加 print
语句
If you want to see what's in the click
event, just put a print
statement around it
observeEvent(input$Map_shape_click, { # update the location selectInput on map clicks
p <- input$Map_shape_click
print(p)
})
它会将对象打印到控制台.
and it will print the object to the console.
这篇关于闪亮的传单 ploygon 点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!