在 shiny 应用程序的 multi.js 输入中将标志与国家/地区匹配
Match flags with countries in a multi.js input of shiny app
我有下面的数据框,其中 multi.js
输入带有国旗和国家。正如您将看到的,很明显这些国家与显示的 glags 不匹配。我该如何解决?
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)
您可以过滤 country_df
数据集以获得所需的 countries2
,而不是使用单独的国家/地区名称和代码向量。然后用 country_df
的 name
和 alpha.2
列替换闪亮代码中出现的所有 countries
和 countries2
。这样做可以确保每个代码都分配给正确的名称:
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)
我有下面的数据框,其中 multi.js
输入带有国旗和国家。正如您将看到的,很明显这些国家与显示的 glags 不匹配。我该如何解决?
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)
您可以过滤 country_df
数据集以获得所需的 countries2
,而不是使用单独的国家/地区名称和代码向量。然后用 country_df
的 name
和 alpha.2
列替换闪亮代码中出现的所有 countries
和 countries2
。这样做可以确保每个代码都分配给正确的名称:
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)