I have the dataframe below with the multi.js
input with flags and countries. As you will see it is obvious that the countries do not match with glags displayed. How can I fix that?
library(shiny)
library(shinyWidgets)
countries_df <- read.csv("https://raw.githubusercontent.com/lukes/ISO-3166-Countries-with-Regional-Codes/master/all/all.csv",strip.white = TRUE)
countries <- tolower(countries_df[,"alpha.2"])
countries2<-c("Afghanistan", "India", "Albania", "Bangladesh", "Sri Lanka",
"Belarus", "North Macedonia", "Serbia", "Ukraine", "Belize",
"Guatemala", "Bhutan", "Chile", "Mexico", "Bulgaria", "Croatia",
"Romania", "Slovenia", "Turkey", "Guyana", "Suriname", "Japan",
"Czechia", "Estonia", "Israel", "Latvia", "Lithuania", "Slovakia",
"Algeria", "Canada", "Colombia", "Costa Rica", "Jordan", "New Zealand",
"Peru", "Barbados", "Dominica", "Grenada", "Jamaica", "Montserrat",
"Bahamas", "Cuba", "Dominican Republic", "Haiti", "Cameroon",
"Central African Republic", "Chad", "Gabon", "El Salvador", "Honduras",
"Nicaragua", "Panama", "Hungary", "Poland", "China", "Egypt",
"Ghana", "Guinea", "Mali", "Morocco", "Tunisia", "Ecuador", "Pakistan",
"Singapore", "Kazakhstan", "Angola", "Burundi", "Comoros", "Djibouti",
"Eritrea", "Eswatini", "Ethiopia", "Kenya", "Lesotho", "Madagascar",
"Malawi", "Mauritius", "Mozambique", "Namibia", "Rwanda", "Sudan",
"Uganda", "Zambia", "Zimbabwe", "Armenia", "Azerbaijan", "Georgia",
"Kyrgyzstan", "Tajikistan", "Turkmenistan", "Uzbekistan", "Benin",
"Botswana", "Burkina Faso", "Cook Islands", "Equatorial Guinea",
"Fiji", "Gambia", "Guinea-Bissau", "Kiribati", "Liberia", "Marshall Islands",
"Mauritania", "Micronesia (Federated States of)", "Nauru", "Niger",
"Nigeria", "Niue", "Palau", "Papua New Guinea", "Samoa", "Senegal",
"Seychelles", "Sierra Leone", "Solomon Islands", "South Africa",
"Togo", "Tonga", "Tuvalu", "Vanuatu", "Cyprus", "Denmark", "Ireland",
"Andorra", "Iraq", "Indonesia", "Malaysia", "Belgium", "France", "Germany", "Italy", "Luxembourg", "Netherlands", "Libya", "Somalia",
"Western Sahara", "Faroe Islands", "Greece", "Austria", "Finland",
"Sweden", "Lebanon", "Malta", "Montenegro", "Portugal", "San Marino",
"Spain", "Liechtenstein", "Bahrain", "Kuwait", "Oman", "Qatar",
"Saudi Arabia", "United Arab Emirates", "Philippines", "Thailand",
"Nepal", "Australia", "Cambodia", "Maldives", "Mongolia")
img_urls <- paste0(
'https://cdn.rawgit.com/lipis/flag-icon-css/master/flags/4x3/',
countries, '.svg'
)
input_widget <- multiInput(
inputId = "Id010",
label = "Countries :",
choices = NULL,
selected = countries2[1],
choiceNames = lapply(
seq_along(countries2),
function(i) {
tagList(
tags$img(src = img_urls[i], width = 20, height = 15),
countries2[i]
)
}
),
choiceValues = countries2
)
ui <- fluidPage(
input_widget
)
server <- function(input, output, session) {
}
shinyApp(ui, server)
CodePudding user response:
Instead of making use of separate vectors of country names and codes you could filter your country_df
dataset for your desired countries2
. Then replace all occurrences of countries
and countries2
in your shiny code by the name
and alpha.2
columns of country_df
. Doing so makes sure that each code gets assigned to the right name:
library(shiny)
library(shinyWidgets)
countries_df <- read.csv("https://raw.githubusercontent.com/lukes/ISO-3166-Countries-with-Regional-Codes/master/all/all.csv", strip.white = TRUE)
countries_df <- countries_df[c("name", "alpha.2")]
countries_df$alpha.2 <- tolower(countries_df$alpha.2)
countries_df <- countries_df[countries_df$name %in% countries2, ]
img_urls <- paste0(
"https://cdn.rawgit.com/lipis/flag-icon-css/master/flags/4x3/",
countries_df$alpha.2, ".svg"
)
input_widget <- multiInput(
inputId = "Id010",
label = "Countries :",
choices = NULL,
selected = countries_df$name[1],
choiceNames = lapply(
seq_along(countries_df$alpha.2),
function(i) {
tagList(
tags$img(src = img_urls[i], width = 20, height = 15),
countries_df$name[i]
)
}
),
choiceValues = countries_df$name
)
ui <- fluidPage(
input_widget
)
server <- function(input, output, session) {
}
shinyApp(ui, server)