R闪亮的updateSelectizeInput标签的自定义HTML标签

克里斯·雪

有没有一种方法可以将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))

谢谢

土卫

如果标签将始终相同,则不要为labelon设置值updateSelectizeInput实际上,您只应设置要更改的参数。

举例来说,这仅更改所选的值:

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

如果label需要更改值,但需要使用标签或样式(例如在这种情况下)h6,则可以使用shinyjs来仅更改标签的文本。为此,您需要idh6标签中添加一个请参见下面的示例,其中第一个观察者内部的标签使用的html功能进行了更改shinyjs我还添加了两个按钮来手动更改标签的文本。

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))

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章