【问题标题】:Shiny dropdown input (selectizeInput) with fontawesome icons带有 fontawesome 图标的闪亮下拉输入 (selectizeInput)
【发布时间】:2021-10-01 04:33:06
【问题描述】:

我想在 Shiny selectizeInput 的项目中包含 font-awesome 图标。我该怎么办?

【问题讨论】:

    标签: r shiny font-awesome selectize.js


    【解决方案1】:

    这是一个函数,selectInputWithIcons,它可以完成这项工作。

    library(shiny)
    library(fontawesome)
    library(htmltools)
    
    
    selectInputWithIcons <- function(
      inputId, inputLabel, labels, values, icons, iconStyle = NULL,
      selected = NULL, multiple = FALSE, width = NULL
    ){
      options <- mapply(function(label, value, icon){
        list(
          "label" = label,
          "value" = value,
          "icon"  = as.character(fa_i(icon, style = iconStyle))
        )
      }, labels, values, icons, SIMPLIFY = FALSE, USE.NAMES = FALSE)
      render <- paste0(
        "{",
        "  item: function(item, escape) {", 
        "    return '<span>' + item.icon + '&nbsp;' + item.label + '</span>';", 
        "  },",
        "  option: function(item, escape) {", 
        "    return '<span>' + item.label + '</span>';", 
        "  }",
        "}"
      )
      widget <- selectizeInput(
        inputId  = inputId, 
        label    = inputLabel,
        choices  = NULL, 
        selected = selected,
        multiple = multiple,
        width    = width,
        options  = list( 
          "options"    = options,
          "valueField" = "value", 
          "labelField" = "label",
          "render"     = I(render),
          "items"     = as.list(selected)
        )
      )
      attachDependencies(widget, fa_html_dependency(), append = TRUE)
    }
    
    
    ui <- fluidPage(
      br(),
      selectInputWithIcons(
        "slctz",
        "Select an animal:",
        labels    = c("I want a dog", "I want a cat"),
        values    = c("dog", "cat"),
        icons     = c("dog", "cat"),
        iconStyle = "font-size: 3rem; vertical-align: middle;",
        selected  = "cat"
      )
    )
    
    server <- function(input, output, session){
      
      observe({
        print(input[["slctz"]])
      })
      
    }
    
    
    shinyApp(ui, server)
    

    【讨论】:

    猜你喜欢
    • 2019-06-28
    • 1970-01-01
    • 1970-01-01
    • 2016-04-04
    • 2021-06-27
    • 2021-09-07
    • 2019-01-24
    • 2018-12-22
    • 1970-01-01
    相关资源
    最近更新 更多