2017-03-14 59 views
4

我有一個地圖叫做business這是從自然地球網站下載。我在這裏做的是我創建了一個顯示地圖的基本地圖輸出。我主要使用admin(這是國家的名稱)和economy這兩列。然後,我在UI下添加了一個名爲Business的下拉列表,這樣當我單擊該國家的多邊形時,該列表將刷新並顯示我點擊的國家/地區。我假設當我寫p <- input$Map_shape_click有光澤會知道p是一個business對象,所以它有列admin和我有這個admin ID引用刷新我的Business下拉列表。但它不起作用。鏈接顯示我所看到的 - 當我點擊不同的國家時,列表不會刷新。閃亮的傳單ploygon點擊事件

enter image description here

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="") 
)))) 
+0

「map_shape_click」的返回值是座標('lat','lng')和'id'(它是'layerId'的值),所以我認爲你想在'p $ id'中代替'p $ admin' – SymbolixAU

+0

你是我的英雄!完全解決問題! – user3833612

+0

不客氣;我已經將其添加爲答案 – SymbolixAU

回答

4

click該事件在小葉返回latlngid(和隨機VA略)。所以你只能訪問其中的一個元素。

id值與您在形狀繪圖功能中指定的layerId有關,因此您的情況爲layerId=~admin

所以你通過點擊的id場acccess的admin

取代p$adminp$id,你應該有你的解決方案。


如果你想看看有什麼在click事件,只是把print聲明周圍

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

,它將打印對象到控制檯。