1
我试图确定在闪亮的世界地图中显示的点击国家。未显示的国家在Plotly转移event_data返回(闪亮)
我用this example for click event和this 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使用的国家列表,我可以用它来移除未知的国家并修复这个转变。
[countrycode](https://github.com/vincentarelbundock/countrycode)是相当不错的。 – alistaire
谢谢@alistaire,但它也包含美属萨摩亚(ASM) 和安道尔(AND) – HubertL
它处理得很好。 – alistaire