selectInput 中的 R Shiny 自定义图标/图像

标签 r shiny

我在 Shiny 的应用程序中有以下代码,让用户可以选择他们想在绘图上使用的点形状。

selectInput("pch", "Point shape",c("15","16","17","18"),selectize = TRUE,multiple=F)

出于审美原因(也是实际原因),我想要 4 个绘图字符的 4 个图像,而不仅仅是数字 15、16、17、18。

同样,在这个例子中,
selectInput("col", "Colour",colours(),selectize = TRUE,multiple=F)

除了颜色的文本名称,我还想使用颜色图像或颜色选择器。

我想到的是以下内容:

enter image description here
enter image description here

谢谢

最佳答案

还有pickerInput来自 shinyWidgets ,可以使用 html/css 自定义。有了它,您可以将任何图像或图标包含到选择小部件中。

但是,使用这种方法,图像必须已经存在。

library(shiny)
library(shinyWidgets)

df <- data.frame(
  val = c("pal1","pal2", "pal3", "pal4")
)

df$img = c(
  sprintf("<img src='https://d9np3dj86nsu2.cloudfront.net/image/eaf97ff8dcbc7514d1c1cf055f2582ad' width=30px><div class='jhr'>%s</div></img>", df$val[1]),
  sprintf("<img src='https://www.color-hex.com/palettes/33187.png' width=30px><div class='jhr'>%s</div></img>", df$val[2]),
  sprintf("<img src='https://www.color-hex.com/palettes/16042.png' width=30px><div class='jhr'>%s</div></img>", df$val[3]),
  sprintf("<img src='https://www.stlawrencegallery.com/wp-content/uploads/2018/09/unique-navy-blue-color-palette-five-stunning-palettes-for-weddings-dark.jpg' width=30px><div class='jhr'>%s</div></img>", df$val[4])
  )


ui <- fluidPage(
  tags$head(tags$style("
                       .jhr{
                       display: inline;
                       vertical-align: middle;
                       padding-left: 10px;
                       }")),
 pickerInput(inputId = "Id0109",
             label = "pickerInput Palettes",
             choices = df$val,
             choicesOpt = list(content = df$img))

  )

server <- function(input, output) {}
shinyApp(ui, server)

enter image description here

关于selectInput 中的 R Shiny 自定义图标/图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30486412/

相关文章:

R:确定每个日期间隔是否与数据框中的所有其他日期间隔重叠

r - 从 R 内部调用 Clojure?

R Shiny 观察行取消选择数据表

IE10 上的 R Shiny 页面

javascript - 更改 R DT 数据表中控件的颜色

r - 在R中按组转置数据

r - 在 Google Cloud App Engine 上部署 Shiny 的应用程序会返回错误

r - tcltk 对话框出现在 RStudio/Shiny Windows 下

r - 在 ggplot2 中按因子级别设置轴中断

css - 更改 Shiny 的红色错误消息的颜色