将国家标志添加到pickerinput shinywidgets。

时间:2017-12-10 17:12:17

标签: r shiny

有一个示例如何将国家/地区标志添加到此处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)

1 个答案:

答案 0 :(得分:6)

您好,您不希望将选项添加为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)

希望这有帮助!