2016-09-30 55 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")