在selectInput中显示闪亮的自定义图标/图像 [英] R shiny custom icon/image in selectInput

查看:10
本文介绍了在selectInput中显示闪亮的自定义图标/图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

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

我的想法是这样的:

谢谢

推荐答案

以下是一个工作示例。此处的目的是在下拉菜单中向用户显示调色板中的颜色(而不仅仅是调色板名称)。在这里,下拉菜单中的图像是在运行时创建的。这可能是不可取的,也可能是不可取的。如果下拉菜单中的图片从未更改(即静电),请参阅世嘉的答案。

这是根据所示的示例here修改的。

ui.R文件

## UI.R

fluidPage(
  title='Plots in Selectize Input',
  tags$h2('Plots in Selectize Input'),
  fluidRow(
    column(4,
           selectizeInput('palette',label="Palette",choices=NULL,options=list(
             placeholder='Select a colour palette',maxOptions=4)
           )),
    column(8,
      plotOutput('plot')
      )
    )
  )

server.R文件

## SERVER.R

library(ggplot2)

data(diamonds)
len <- length(levels(diamonds$cut))
clist <- list("rainbow"=rainbow(len),"topo"=topo.colors(len),
              "terrain"=terrain.colors(len),"cm"=cm.colors(len))

function(input,output,session) {

  paletteurl <- session$registerDataObj(

    name='uniquename1',
    data=clist,
    filter=function(data,req) {

      query <- parseQueryString(req$QUERY_STRING)
      palette <- query$palette
      cols <- clist[[palette]]

      image <- tempfile()
      tryCatch({
        png(image,width=100,height=50,bg='transparent')
        par(mar=c(0,0,0,0))
        barplot(rep(1,length(cols)),col=cols,axes=F)
      },finally = dev.off())

      shiny:::httpResponse(
        200,'image/png',readBin(image,'raw',file.info(image)[,'size'])
      )
    }
  )

  updateSelectizeInput(
    session,'palette',server=TRUE,
    choices=names(clist),
    selected=1,
    options=list(render=I(sprintf(
      "{
        option: function(item, escape) {
        return '<div><img width="100" height="50" ' +
        'src="%s&palette=' + escape(item.value) + '" />' +
        escape(item.value) + '</div>';
        }
      }",
      paletteurl
    )))
    )

  output$plot <- renderPlot({
    shiny::req(input$palette)

    cols <- clist[[input$palette]]
    ggplot(diamonds,aes(x=carat,y=price,colour=cut))+
      geom_point()+
      scale_colour_manual(values=cols)+
      theme_minimal(base_size=18)
  })

}

如果有人更了解这一点,欢迎您改进/更新此答案。甚至添加另一个答案以显示不同的用法。

这篇关于在selectInput中显示闪亮的自定义图标/图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆