您可以执行以下操作,但斜体只显示在列表中。
library(shiny)
ui <- fluidPage(
mainPanel(
selectizeInput(inputId = "tree", label = "Select a Tree", choices = NULL),
# Print selected tree
verbatimTextOutput("selection")
)
)
server <- function(input, output, session){
my_list <- reactive({
list(`Red Oak - Quercus rubra (11671)` = "red",
`Bur Oak - Quercus macrocarpa (2705)` = "bur")
})
observe({
updateSelectizeInput(session, "tree",
choices = my_list(),
options = list(render = I(
'{
option: function(item, escape) {
var splittedLabel = escape(item.label).split(" - ");
return "<div>" + splittedLabel[0] + " - <i>" + splittedLabel[1] + "</i></div>"
}
}'
)))
})
output$selection <- renderPrint({
input$tree
})
}
shinyApp(ui = ui, server = server)
我不知道你是否能为所选的选项找到一个样式。我见过这种技巧
here
(但我花了一些时间才达到这个结果,我发现医生不太清楚)。
编辑
我还找到了如何获取所选选项的样式:
observe({
updateSelectizeInput(session, "tree",
choices = my_list(),
options = list(render = I(
'{
item: function(item, escape) {
var splittedLabel = escape(item.label).split(" - ");
return "<div>" + splittedLabel[0] + " - <i>" + splittedLabel[1] + "</i></div>"
},
option: function(item, escape) {
var splittedLabel = escape(item.label).split(" - ");
return "<div>" + splittedLabel[0] + " - <i>" + splittedLabel[1] + "</i></div>"
}
}'
))
)
})
编辑2
也许最好直接在列表中键入HTML代码:
my_list <- reactive({
list(`Red Oak - <i>Quercus rubra</i> (11671)` = "red",
`Bur Oak - <i>Quercus macrocarpa</i> (2705)` = "bur")
})
然后javascript代码很简单:
observe({
updateSelectizeInput(session, "tree",
choices = my_list(),
options = list(render = I(
'{
item: function(item, escape) {
return "<div>" + item.label + "</div>"
},
option: function(item, escape) {
return "<div>" + item.label + "</div>"
}
}'
))
)
})