你可以做這樣的事情與包shinyWidgets
(這個答案是偏頗的,我這個包的作者):
library("shiny")
library("shinyWidgets")
ui <- fluidPage(
br(),
pickerInput(
inputId = "one",
label = "Choose:",
choices = c("red", "blue", "green"),
choicesOpt = list(content = sprintf(
"<div style='background: %s;'> </div>",
c("red", "blue", "green")
))
),
verbatimTextOutput(outputId = "resone"),
pickerInput(
inputId = "two",
label = "Choose:",
choices = c("home", "search", "ok-sign"),
choicesOpt = list(
icon = c("glyphicon-home",
"glyphicon-search",
"glyphicon-ok-sign")
)
),
verbatimTextOutput(outputId = "restwo")
)
server <- function(input, output, session) {
output$resone <- renderPrint(input$one)
output$restwo <- renderPrint(input$two)
}
shinyApp(ui = ui, server = server)
與selectizeInput
一個解決辦法是把你的圖片名爲www
文件夾中在你的應用目錄中,然後你可以這樣做:
library("shiny")
# dummies images
png(filename = "www/red.png")
plot.new()
rect(0, 0, 1, 1, col = "red")
dev.off()
png(filename = "www/blue.png")
plot.new()
rect(0, 0, 1, 1, col = "blue")
dev.off()
# images are displayed only in dropdown menu
ui <- fluidPage(
br(),
selectizeInput(
'colours', '',
choices = c("blue" = "blue.png", "red" = "red.png"),
selected = "blue",
options = list(
render = I(
"{
option: function(item, escape) {
return '<div><img src=\"' + item.value + '\" width = 20 />' + escape(item.name) + '</div>'
}
}")
)
)
)
server <- function(input, output, session) {
}
shinyApp(ui = ui, server = server)
非常感謝!使用'shinyWidgets'軟件包,一切都很好! – Adela
不錯!很高興聽你這樣說 – Victorp