Chr*_*hee 2 shiny selectize.js
有没有办法将 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))
Run Code Online (Sandbox Code Playgroud)
谢谢
如果标签始终相同,则不要为labelon设置值updateSelectizeInput。实际上,您应该只设置您想要更改的参数。
例如,这只是更改选定的值:
updateSelectizeInput(session, 'DropDownSelectize', selected = "choice3")
Run Code Online (Sandbox Code Playgroud)
label如果需要更改的值,但需要使用标签或样式(如本例所示h6),则可以shinyjs仅更改标签的文本。为此,您需要id向h6标签添加 。请参阅下面的示例,其中在第一个观察者内部使用 函数更改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))
Run Code Online (Sandbox Code Playgroud)