R语言 闪亮的下拉式输入,带有用图标装饰的组

l2osamch  于 2023-03-05  发布在  其他
关注(0)|答案(1)|浏览(83)

this answer的一条评论中,@Vlad询问如何在一个Shiny应用程序中获得一个下拉式输入 *,其中的分组 * 用图标装饰。没有给出答案,所以我问了这个问题。

mwkjh3gx

mwkjh3gx1#

下面是一个带有shinySelect包的解决方案。

library(shiny)
library(shinySelect)
library(fontawesome)
library(bslib)

states <- HTMLgroupedChoices(
  groups = mapply(
    function(x, icon){
      tags$h2(
        fa_i(icon), x, 
        style="text-decoration: underline; color: darkred;"
      )
    },
    list("East Coast", "West Coast", "Midwest"), 
    list("hamburger", "pizza-slice", "fish"),
    SIMPLIFY = FALSE
  ),
  labels = list(
    lapply(list("NY", "NJ", "CT"), function(x){
      tags$span(HTML("&bull;"), x, style="color: red;")
    }),
    lapply(list("WA", "OR", "CA"), function(x){
      tags$span(HTML("&bull;"), x, style="color: green;")
    }),
    lapply(list("MN", "WI", "IA"), function(x){
      tags$span(HTML("&bull;"), x, style="color: blue;")
    })
  ),
  values = list(
    list("NY", "NJ", "CT"),
    list("WA", "OR", "CA"),
    list("MN", "WI", "IA")
  )
)

ui <- fluidPage(
  theme = bs_theme(version = 4),
  titlePanel("Groups with icons example"),
  sidebarLayout(
    sidebarPanel(
      selectControlInput(
        "select",
        label = tags$h3("Choose some states", style="color: crimson;"),
        containerClass = NULL,
        choices = states,
        selected = c("NY", "OR"),
        multiple = TRUE,
        animated = TRUE
      )
    ),
    mainPanel(
      verbatimTextOutput("textOutput")
    )
  )
)

server <- function(input, output, session) {
  output[["textOutput"]] <- renderPrint({
    sprintf("You selected: %s.", toString(input[["select"]]))
  })
}

shinyApp(ui, server)

相关问题