【问题标题】:use html in selectizeInput with R shiny在带有 R 闪亮的 selectizeInput 中使用 html
【发布时间】:2021-06-27 06:59:23
【问题描述】:

我想在 select(ize)Input 的选项中使用一些 html。有谁知道如何告诉闪亮将选项视为 HTML 的简单解决方案?

library(shiny)

ui <- fluidPage(
  selectInput("test html use", label = "option", choices = c("<div title = 'This is option A'>opt A</div>", "opt B"))
)

server <- function(input, output) {
}

shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: r shiny selectinput


    【解决方案1】:

    为了将selectizeInput 的选项视为HTML,render 选项是要走的路。以下代码将呈现普通输出:

    library(shiny)
    
    shinyApp(
      ui = fluidPage(
        br(),
        selectizeInput(
          "slctz", "Select something:",
          choices = list("option1" = "value1", "option2" = "value2"), 
          options = list( 
            render = I("{
            item: function(item, escape) { 
              return '<span>' + item.label + '</span>'; 
            },
            option: function(item, escape) { 
              return '<span>' + item.label + '</span>'; 
            }
          }")
          )
        )
      ),
      server = function(input, output) {}
    )
    

    option 字段用于选项列表,而item 字段用于选定选项。

    因此,如果您想要设置选项和选定选项的样式,您可以通过将class 属性添加到span 元素并在UI 中定义您的CSS 类来简洁地做到这一点:

    ui = fluidPage(
      tags$head(
        tags$style(
          HTML(
            "
            .myoption {......}
            .myitem {......}
            "
          )
        )
      ),
      br(),
      selectizeInput(
        "slctz", "Select something:",
        choices = list("option1" = "value1", "option2" = "value2"), 
        options = list( 
          render = I("{
            item: function(item, escape) { 
              return '<span class=\"myitem\">' + item.label + '</span>'; 
            },
            option: function(item, escape) { 
              return '<span class=\"myoption\">' + item.label + '</span>'; 
            }
          }")
        )
      )
    )
    

    【讨论】:

    • 您能否根据shiny.rstudio.com/articles/selectize.html 分享这些疑问? (1) 它只提到option: function,而不是item: function,你能解释一下它的作用吗? (2) 它给出了一个使用choices = cbind(name = rownames(mtcars), mtcars) 的例子,但它对我不起作用。您能否提供一个将数据框传递给选项的示例?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-05
    • 1970-01-01
    • 1970-01-01
    • 2019-06-28
    • 2021-09-07
    • 2019-01-24
    相关资源
    最近更新 更多