R shiny updateSelectizeInput 标签的自定义 HTML 标签

R shiny updateSelectizeInput custom HTML tags for labels

有没有办法将 HTML 标签(例如 h6)用于 updateSelectizeInput(适用于 selectInput,请参见下面的代码)?使用下面的代码,只需在 updateSelectizeInput [object Object] 中使用 h6("Label") 即可显示为输出。

rm(list = ls())
library(shiny)



ui =fluidPage(
  selectizeInput('DropDownSelectize',choices=NULL,label=""),
  selectInput('DropDownSelect',choices = c("choice1","choice2","choice3"),
  label=h6("Label"))
 )

server = function(input, output, session) {

 observe({
   updateSelectizeInput(session, 
                     'DropDownSelectize',
                      label = h6("Label"),
                      choices = c("choice1","choice2","choice3"),
                      selected = "choice1",
                      server = TRUE)

 })


}
runApp(list(ui = ui, server = server))

谢谢

如果标签要始终相同,请不要在 updateSelectizeInput 上设置 label 的值。实际上,您应该只设置要更改的参数。

举例来说,这只是更改选定的值:

updateSelectizeInput(session, 'DropDownSelectize', selected = "choice3")

如果需要更改 label 的值但需要更改标签或样式,例如本例 h6,您可以使用 shinyjs 仅更改文本标签。为此,您需要将 id 添加到 h6 标签。请参见下面的示例,其中在第一个观察者中使用 shinyjshtml 函数更改了标签。我还添加了两个按钮来手动更改标签的文本。

library(shiny)
library(shinyjs)

ui =fluidPage(
  shinyjs::useShinyjs(), # to initialize shinyjs
  selectizeInput('DropDownSelectize',choices=NULL,label=h6("", id = "labelText")),
  selectInput('DropDownSelect',choices = c("choice1","choice2","choice3"),
  label=h6("Label")),
  actionButton("useLabel1", "Use Label 1"),
  actionButton("useLabel2", "Use Label 2")
 )

server = function(input, output, session) {
  observe({
    updateSelectizeInput(session, 
                     'DropDownSelectize',
                      # label = h6("Label"), # no needed 
                      choices = c("choice1","choice2","choice3"),
                      selected = "choice1",
                      server = TRUE)
    shinyjs::html("labelText", "Label")
  })

  observeEvent(input$useLabel1, {
    shinyjs::html("labelText", "Label 1")
  })  

  observeEvent(input$useLabel2, {
    shinyjs::html("labelText", "Label 2")
  })  

}
runApp(list(ui = ui, server = server))