gpt4 book ai didi

javascript - selectizeInput 带有图像/图标

转载 作者:行者123 更新时间:2023-12-01 02:57:29 28 4
gpt4 key购买 nike

我想知道是否有可能在 Shiny 中创建类似“selectIcon”的东西。我想要一个仅包含图标或例如的选择器颜色。

selectizeInput('colours', '',
choices = c("blue", "red"),
selected = "blue")

但是我想显示彩色方 block ,而不是“蓝色”和“红色”。所选选项也应如此。假设我的所有选项都有 .png 文件。如何将这些文件包含在 selectizeInput() 中?

这是一个与 this one 非常相似的问题但是,由于我不了解 js,所以没有适合我的解决方案。

我尝试过这样的事情

  selectizeInput('colours', '',
choices = c("blue", "red"),
selected = "blue",
options = list(render = I(
"{
option: function(item, escape) {
return '<div><img src=\"item.png\" width = 20 />' + escape(item.name) + '</div>'
}
}"))

但没有任何成功。这些选项现在未定义。没有显示数字。

感谢任何帮助。

最佳答案

您可以使用包 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;'>&nbsp;</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)

编辑:在最新版本的shiny中,将item.name替换为item.label

关于javascript - selectizeInput 带有图像/图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46635054/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com