HubertL HubertL - 2 months ago 27
R Question

Unknown country code breaks event_data selection in Plotly (Shiny)

I'm trying to determine clicked country in a world map displayed in shiny.

I used this example for click event and this one for display and this code works:

library(shiny)
library(plotly)

ui <- shinyUI(fluidPage(plotlyOutput("plot"),textOutput("text")))

server <- shinyServer(function(input, output) {
df <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_world_gdp_with_codes.csv')
output$text <- renderPrint({
event.data <- event_data("plotly_click")
if(!is.null(event.data)) event.data else NULL})


output$plot <- renderPlotly({
plot_geo(df) %>%
add_trace(z = ~GDP..BILLIONS., color = ~GDP..BILLIONS., text = ~COUNTRY, locations = ~CODE)
})
})

shinyApp(ui = ui, server = server)


When I click on one of the countries from the first entries of the dataset:

Afghanistan,21.71,AFG
Albania,13.40,ALB
Algeria,227.80,DZA


I get correct selection, where pointNumber+1 is the row number in the dataset:

curveNumber pointNumber z
1 0 0 21.71

curveNumber pointNumber z
1 0 1 13.4

curveNumber pointNumber z
1 0 2 227.8


But if I click on Angola which is after 2 undisplayed countries :

American Samoa,0.75,ASM
Andorra,4.80,AND
Angola,131.40,AGO


I get wrong selection which actually is American Samoa:

curveNumber pointNumber z
1 0 3 0.75


So I'm guessing that the selection is calculated on a list of coutries that are known by plotly, projected on original dataset (which include unknown countries)

I think that I could use the list of country codes currently known by Plotly as a work-around, but I don't know where to find it.

Answer

A work-around is to use a country list like the one included in package countrycode and plot it, then remove each country that is not (currently) handled by plotly. This gave me this list:

CountriesInPlotly <- structure(list(COUNTRY = c("Afghanistan", "Angola", "Albania", 
    "United Arab Emirates", "Argentina", "Armenia", "Australia", 
    "Austria", "Azerbaijan", "Burundi", "Belgium", "Benin", "Burkina Faso", 
    "Bangladesh", "Bulgaria", "Belarus", "Bolivia", "Brazil", "Brunei", 
    "Bhutan", "Botswana", "Canada", "Switzerland", "Chile", "China", 
    "Ivory Coast", "Cameroon", "Colombia", "Costa Rica", "Cuba", 
    "Cyprus", "Czech Republic", "Germany", "Djibouti", "Denmark", 
    "Dominican Republic", "Algeria", "Ecuador", "Egypt", "Eritrea", 
    "Western Sahara", "Spain", "Estonia", "Ethiopia", "Finland", 
    "Fiji", "France", "Gabon", "United Kingdom", "Georgia", "Ghana", 
    "Guinea", "Gambia", "Guinea-Bissau", "Equatorial Guinea", "Greece", 
    "Greenland", "Guatemala", "Guyana", "Honduras", "Croatia", "Haiti", 
    "Hungary", "Indonesia", "India", "Ireland", "Iran", "Iraq", "Iceland", 
    "Israel", "Italy", "Jamaica", "Jordan", "Japan", "Kazakhstan", 
    "Kenya", "Kyrgyzstan", "Cambodia", "South Korea", "Kuwait", "Laos", 
    "Lebanon", "Liberia", "Libya", "Sri Lanka", "Lesotho", "Lithuania", 
    "Luxembourg", "Latvia", "Morocco", "Moldova", "Madagascar", "Mexico", 
    "Macedonia", "Mali", "Myanmar", "Montenegro", "Mongolia", "Mozambique", 
    "Mauritania", "Malawi", "Malaysia", "Namibia", "Niger", "Nigeria", 
    "Nicaragua", "Netherlands", "Norway", "Nepal", "New Zealand", 
    "Oman", "Pakistan", "Panama", "Peru", "Philippines", "Papua New Guinea", 
    "Poland", "Puerto Rico", "North Korea", "Portugal", "Paraguay", 
    "Palestine", "Qatar", "Romania", "Russia", "Rwanda", "Saudi Arabia", 
    "Sudan", "Senegal", "Solomon Islands", "Sierra Leone", "El Salvador", 
    "Somalia", "Serbia", "South Sudan", "Suriname", "Slovakia", "Slovenia", 
    "Sweden", "Swaziland", "Syria", "Chad", "Togo", "Thailand", "Tajikistan", 
    "Turkmenistan", "East Timor", "Tunisia", "Turkey", "Taiwan", 
    "Tanzania", "Uganda", "Ukraine", "Uruguay", "United States", 
    "Uzbekistan", "Venezuela", "Vietnam", "Vanuatu", "Yemen", "South Africa", 
    "Zambia", "Zimbabwe"), CODE = c("AFG", "AGO", "ALB", "ARE", "ARG", 
    "ARM", "AUS", "AUT", "AZE", "BDI", "BEL", "BEN", "BFA", "BGD", 
    "BGR", "BLR", "BOL", "BRA", "BRN", "BTN", "BWA", "CAN", "CHE", 
    "CHL", "CHN", "CIV", "CMR", "COL", "CRI", "CUB", "CYP", "CZE", 
    "DEU", "DJI", "DNK", "DOM", "DZA", "ECU", "EGY", "ERI", "ESH", 
    "ESP", "EST", "ETH", "FIN", "FJI", "FRA", "GAB", "GBR", "GEO", 
    "GHA", "GIN", "GMB", "GNB", "GNQ", "GRC", "GRL", "GTM", "GUY", 
    "HND", "HRV", "HTI", "HUN", "IDN", "IND", "IRL", "IRN", "IRQ", 
    "ISL", "ISR", "ITA", "JAM", "JOR", "JPN", "KAZ", "KEN", "KGZ", 
    "KHM", "KOR", "KWT", "LAO", "LBN", "LBR", "LBY", "LKA", "LSO", 
    "LTU", "LUX", "LVA", "MAR", "MDA", "MDG", "MEX", "MKD", "MLI", 
    "MMR", "MNE", "MNG", "MOZ", "MRT", "MWI", "MYS", "NAM", "NER", 
    "NGA", "NIC", "NLD", "NOR", "NPL", "NZL", "OMN", "PAK", "PAN", 
    "PER", "PHL", "PNG", "POL", "PRI", "PRK", "PRT", "PRY", "PSE", 
    "QAT", "ROU", "RUS", "RWA", "SAU", "SDN", "SEN", "SLB", "SLE", 
    "SLV", "SOM", "SRB", "SSD", "SUR", "SVK", "SVN", "SWE", "SWZ", 
    "SYR", "TCD", "TGO", "THA", "TJK", "TKM", "TLS", "TUN", "TUR", 
    "TWN", "TZA", "UGA", "UKR", "URY", "USA", "UZB", "VEN", "VNM", 
    "VUT", "YEM", "ZAF", "ZMB", "ZWE")), .Names = c("COUNTRY", "CODE"
    ), row.names = c(NA, -167L), class = c("data.table", "data.frame"
    ))