gpt4 book ai didi

r - Plotly shift event_data 返回中未显示的国家( Shiny )

转载 作者:行者123 更新时间:2023-12-05 07:47:23 26 4
gpt4 key购买 nike

我正在尝试确定在 Shiny 显示的世界地图中点击的国家/地区。

我用了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")})
}))

当我点击数据集的第一个国家阿富汗时,它正确地选择了它

但是如果我点击第三个Albania,它会返回第二个Aland Islands,Plotly 不会显示它。

所以我猜测选择是根据我在原始数据集(包括所有国家/地区)上投影的显示条目列表计算的

我试图在 github 上找到 Plotly 使用的国家/地区列表,但没有成功,我可以用它来删除未知国家/地区并修复转变。

最佳答案

一种变通方法是使用像包 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")

关于r - Plotly shift event_data 返回中未显示的国家( Shiny ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39801532/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com