如果标签始终相同,则不要设置值label
on updateSelectizeInput
。实际上,您应该只设置您想要更改的参数。
例如,这只是更改选定的值:
updateSelectizeInput(session, 'DropDownSelectize', selected = "choice3")
如果值label
需要更改,但需要使用标签或样式,就像在本例中一样h6
, 您可以使用shinyjs
仅更改标签的文本。为此,您需要添加一个id
to the 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))