我有一個地圖叫做business
這是從自然地球網站下載。我在這裏做的是我創建了一個顯示地圖的基本地圖輸出。我主要使用admin
(這是國家的名稱)和economy
這兩列。然後,我在UI下添加了一個名爲Business
的下拉列表,這樣當我單擊該國家的多邊形時,該列表將刷新並顯示我點擊的國家/地區。我假設當我寫p <- input$Map_shape_click
有光澤會知道p是一個business
對象,所以它有列admin
和我有這個admin
ID引用刷新我的Business
下拉列表。但它不起作用。鏈接顯示我所看到的 - 當我點擊不同的國家時,列表不會刷新。閃亮的傳單ploygon點擊事件
server.r
country <- readOGR(dsn = tmp, layer = "ne_110m_admin_0_countries", encoding = "UTF-8")
business<-country[[email protected]$admin %in% c("Brazil","Colombia","Panama","Kazakhstan","Argentina","India","","Chile","Dominican Republic","United Kingdom","El Salvador","United States of America"),]
[email protected]$category <- factor(sample.int(20L, nrow([email protected]), FALSE))
shinyServer(function(input, output,session) {
output$Map <- renderLeaflet({
factpal <- colorFactor(topo.colors(20), [email protected]$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="")
))))
「map_shape_click」的返回值是座標('lat','lng')和'id'(它是'layerId'的值),所以我認爲你想在'p $ id'中代替'p $ admin' – SymbolixAU
你是我的英雄!完全解決問題! – user3833612
不客氣;我已經將其添加爲答案 – SymbolixAU