2016-09-30 49 views
1

我試圖確定在閃亮的世界地圖中顯示的點擊國家。未顯示的國家在Plotly轉移event_data返回(閃亮)

我用this example for click eventthis one for display與此代碼的工作:

library(shiny) 
library(plotly) 
library(countrycode) 

df <- countrycode_data[,c("iso3c", "country.name")] 
df <- df[complete.cases(df),] 
df$random <- rnorm(dim(df)[1]) 

shinyApp(
    ui = shinyUI(fluidPage(plotlyOutput("plot"), textOutput("text"))), 

    server = shinyServer(function(input, output) { 
    output$text <- renderPrint({ 
     d <- event_data("plotly_click") 
     ifelse(is.null(d), 
       "No selection", 
       as.character(df[as.numeric(d[2])+1,"country.name"])) 
     }) 

    output$plot <- renderPlotly({plot_ly(df, z=~random, locations=~iso3c, 
            text=~country.name, type="choropleth")}) 
    })) 

當我點擊阿富汗這是該數據集的第一個國家,它正確地選擇它

但是,如果我點擊阿爾巴尼亞這是第三個,它返回奧蘭羣島這是第二個,而不是由Plotly顯示。

所以我猜測,選擇顯示的條目,我對原始數據集項目(包括所有國家)的列表計算

我不成功嘗試在GitHub上找到Plotly使用的國家列表,我可以用它來移除未知的國家並修復這個轉變。

+0

[countrycode](https://github.com/vincentarelbundock/countrycode)是相當不錯的。 – alistaire

+0

謝謝@alistaire,但它也包含美屬薩摩亞(ASM) 和安道爾(AND) – HubertL

+0

它處理得很好。 – alistaire

回答

0

解決方法是使用包含在包countrycode中的國家/地區列表並繪製它,然後刪除不是(當前)由plotly處理的每個國家/地區。這給了我這個清除ISO代碼列表:

CountriesNotInPlotly <- structure(list(
iso3c = c("ALA", "ASM", "AND", "AIA", "ATA", "ATG", 
"ABW", "BHR", "BRB", "BMU", "BES", "BIH", "BVT", "IOT", "CPV", 
"CYM", "CAF", "CXR", "CCK", "COM", "COD", "COG", "COK", "CUW", 
"CSK", "DMA", "FLK", "FRO", "GUF", "PYF", "DDR", "GIB", "GRD", 
"GLP", "GUM", "GGY", "HMD", "VAT", "HKG", "IMN", "JEY", "KIR", 
"LIE", "MAC", "MDV", "MLT", "MHL", "MTQ", "MUS", "MYT", "FSM", 
"MCO", "MSR", "NRU", "ANT", "NIU", "NFK", "MNP", "PLW", "PCN", 
"REU", "BLM", "SHN", "KNA", "LCA", "MAF", "SPM", "VCT", "WSM", 
"SMR", "STP", "SYC", "SGP", "SXM", "SGS", "SJM", "TKL", "TON", 
"TTO", "TCA", "TUV", "UMI", "VGB", "VIR", "WLF", "YMD", "YUG", 
"EAZ")), .Names = "iso3c", row.names = c(NA, -88L), class = "data.frame")