Search code examples
rshinyshinywidgets

Create a multi.sj shiny widget with country flags in shiny app


Im trying to create a multi.js shiny widget like this here. But I miss the flags object. Anyone knows where can I find and load it?

library(shiny)
library(shinyWidgets)
 countries<-c("Belgium","Greece","Brazil")

ui <- fluidPage(
  multiInput(
    inputId = "Id010",
    label = "Countries :", 
    choices = NULL,
    choiceNames = lapply(seq_along(countries), 
                         function(i) tagList(tags$img(src = flags[i],
                                                      width = 20, 
                                                      height = 15), countries[i])),
    choiceValues = countries
  )
)

server <- function(input, output) {
  
  
  
}

shinyApp(ui, server)

Solution

  • Based on chrome's devtools, it seems the images in the shinyWidgets gallery are pulled from urls like this one here: https://cdn.rawgit.com/lipis/flag-icon-css/master/flags/4x3/de.svg.

    If you want to include this into the input widget, you'll need to define a character vector that holds the corresponding urls

    countries <- c('de', 'at', 'br')
    
    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,
      choiceNames = lapply(
        seq_along(countries), 
        function(i) {
          tagList(
            tags$img(src = img_urls[i], width = 20, height = 15), 
            countries[i]
          )
        }
      ),
      choiceValues = countries
    )
    

    Then you can place the input widget anywhere in you UI

    shinyApp(fluidPage(input_widget), function(...) {})