首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >添加国家标志到选择器输入shinywidgets。

添加国家标志到选择器输入shinywidgets。
EN

Stack Overflow用户
提问于 2017-12-11 01:12:17
回答 1查看 1.5K关注 0票数 4

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

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

我正在尝试稍微调整代码,以使用shinywidgets中的pickerinput实现相同的结果。然而,在我的结果中,我没有看到任何图像。

代码语言:javascript
复制
  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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-13 22:59:34

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

代码语言:javascript
复制
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)

希望这能有所帮助!

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47741321

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档