【问题标题】:Adding country flag to pickerinput shinywidgets.将国家标志添加到 pickerinput 闪亮小部件。
【发布时间】:2020-03-31 12:37:24
【问题描述】:

这里有一个如何将国家标志添加到checkBoxGroupInput 的示例

https://gist.github.com/bborgesr/f2c865556af3b92e6991e1a34ced2a4a

我正在尝试使用来自 shinywidgets 的pickerinput 稍微调整代码以达到相同的结果。但是,在我的结果中,我没有看到任何图像。

  library(shiny)
  library(shinyWidgets)

  countries <- c("Australia", "United Kingdom", "United States")

  flags <- c(
    "https://cdn.rawgit.com/lipis/flag-icon-css/master/flags/4x3/au.svg",
    "https://cdn.rawgit.com/lipis/flag-icon-css/master/flags/4x3/gb.svg",
    "https://cdn.rawgit.com/lipis/flag-icon-css/master/flags/4x3/us.svg"
  )

  ui <- fluidPage(

    pickerInput("countries", "countries", multiple = T,
                choices = countries,

                choicesOpt = list(content =  
                                    mapply(countries, flags, FUN = function(country, flagUrl) {
                                      tagList(
                                        tags$img(src=flagUrl, width=20, height=15),
                                        country
                                      )
                                    }, SIMPLIFY = FALSE, USE.NAMES = FALSE)

                                    ))
    ,

  textOutput("txt")
  )

    server <- function(input, output, session) {
    output$txt <- renderText({
      paste("You chose", paste(input$countries, collapse = ", "))
    })
  }

  shinyApp(ui, server)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    您好,您不想将选项添加为 tagList 而是像这样的 HTML 字符串

    library(shiny)
    library(shinyWidgets)
    
    countries <- c("Australia", "United Kingdom", "United States")
    
    flags <- c(
      "https://cdn.rawgit.com/lipis/flag-icon-css/master/flags/4x3/au.svg",
      "https://cdn.rawgit.com/lipis/flag-icon-css/master/flags/4x3/gb.svg",
      "https://cdn.rawgit.com/lipis/flag-icon-css/master/flags/4x3/us.svg"
    )
    
    ui <- fluidPage(
    
      pickerInput("countries", "countries", multiple = T,
                  choices = countries,
    
                  choicesOpt = list(content =  
                                      mapply(countries, flags, FUN = function(country, flagUrl) {
                                        HTML(paste(
                                          tags$img(src=flagUrl, width=20, height=15),
                                          country
                                        ))
                                      }, SIMPLIFY = FALSE, USE.NAMES = FALSE)
    
                  ))
      ,
    
      textOutput("txt")
    )
    
    server <- function(input, output, session) {
      output$txt <- renderText({
        paste("You chose", paste(input$countries, collapse = ", "))
      })
    }
    
    shinyApp(ui, server)
    

    希望这会有所帮助!

    【讨论】:

    • Barton 确实有帮助,谢谢。但是有一个问题,是否可以删除国家名称并只保留国旗,但包括 options = list(live-search = T) 并搜索“澳大利亚”例如并显示国旗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-30
    • 1970-01-01
    • 1970-01-01
    • 2020-02-18
    • 1970-01-01
    • 2017-06-25
    • 2020-04-04
    相关资源
    最近更新 更多