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

标签 r shiny plotly

我正在尝试确定在 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/

相关文章:

r - NA 用于网络抓取中的空白

r - 如何将数据框几何图形保存到 R 中的 shapefile

r - 在使用粘贴的公式创建的LME拟合上,方差分析测试失败

r - 如何解释 h2o.predict() 结果的概率 (p0, p1)

R Shiny : Edit the format of a table output

r - 在 Shiny/Leaflet/R 中按缩放级别更改标记聚类

r - 在 R Shiny 输入中按 Enter 键选择多个搜索关键字项目

python - 在Python中以绘图方式勾勒出绘图区域

r - Plotly 中的树形图和标签

r - R 中使用 Plotly 绘制多个 y 轴图表