将国家标志添加到pickerinput shinywidgets.

MLE*_*LEN 3 r shiny

有一个例子如何在checkBoxGroupInput这里添加国家标志

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

我试图稍微调整代码,以使用pickerinputshinywidgets 实现相同的结果.但是,在我的结果中,我没有看到任何图像.

  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)
Run Code Online (Sandbox Code Playgroud)

小智 8

嗨,你不想添加选项作为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)
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!