问题
Is there a way to use HTML tags (e.g. h6) for updateSelectizeInput (which is working for selectInput, see code below)? With the code below simply using h6("Label") in the updateSelectizeInput [object Object] is shown as output.
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))
Thanks
回答1:
If the label is going to be always the same, just don't set a value for label on updateSelectizeInput. Actually, you should only set the parameters that you want to change.
By instance, this is only changing the selected value:
updateSelectizeInput(session, 'DropDownSelectize', selected = "choice3")
If the value of label needs to be changed but with a tag or style, like in this case h6, you can use shinyjs to change only the text of the label. For that purpose you need to add an id to the h6 tag. See the example below where inside the first observer the label is changed using the html function of shinyjs. I also added two buttons to manually change the text of the label.
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))
来源:https://stackoverflow.com/questions/40341804/r-shiny-updateselectizeinput-custom-html-tags-for-labels