闪亮的服务器中的动态颜色输入 [英] Dynamic color input in shiny server

查看:82
本文介绍了闪亮的服务器中的动态颜色输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Shiny创建一个应用程序,我希望用户能够选择绘图中每条线的颜色.一般的想法是将数据导入应用程序,然后在数据中绘制每个变量.我尝试使用Shinysky软件包中的colorpicker'jscolorInput',将其放置在ui.r文件中时效果很好,但是由于我希望我的应用程序对于上传的每个数据集都是动态的,因此需要将colorpicker放入服务器中. R,使用反应函数. 当放置在服务器中时,"jscolorInput"不起作用.

I am trying to create an app using Shiny, where I want the user to be able to select the color of each line in a plot. The general idea is to import the data in the app and then plot each variable in the data. I tried to use the colorpicker 'jscolorInput' from the shinysky package, which works fine when placed in the ui.r file, but since I want my app to be dynamic for each dataset uploaded, I need to put the colorpicker in the server.R, using a reactive function. When placed in the server, the 'jscolorInput' does not work.

我想做的是:

  1. 重现颜色选择器的次数是 数据中的变量
  2. 从颜色中获取输入并传递它 作为情节中的颜色参数
  1. Reproduce the colorpicker as many times as the number of variables in the data
  2. Take the input from the color and pass it as color argument in the plot

我在闪亮的开发和stackoverflow方面都是新手,所以请原谅我的错误.

I am very new in both shiny development and stackoverflow, so please excuse my mistakes.

这是一个可复制的示例,不起作用.

Here is a reproducible example that does not work.

require(shinysky)
require(shiny)

dat <- data.frame(matrix(rnorm(120, 2, 3), ncol=3))

runApp(list(
ui = bootstrapPage(
    # The reactive colorpicker
    uiOutput('myPanel'),
    # The plot
    plotOutput('plot')
),
server = function(input, output) {
    # Print as many colorpickers as the columns in the dataset
    cols <- reactive({
        n <- ncol(dat)
        for(i in 1:n){
            print(jscolorInput(paste("col", i, sep="_")))    
        }
    })

        output$myPanel <- renderPrint({cols()})
    # Put all the input in a vector
    colors <- reactive({
        n <- ncol(dat)
        lapply(1:n, function(i) {
            input[[paste("col", i, sep="_")]]
        })
    })

    output$plot <- renderPlot({
        cols <- ifelse(is.null(input$col_1), rep("000000 ", n), colors()) 
        plot(dat[,1], col= paste0("#", cols[1], ""))
                                for(i in 2:ncol(dat))lines(dat[,i], col=cols[i])
    })

}
))

推荐答案

此处是您要执行的操作的工作版本.看一下我们的代码之间的差异,您的代码有一些问题.另外,请注意,我不使用shinysky,因为它不再具有拾色器(已移至另一个处于非活动状态的程序包),所以我使用的是

Here is a working version of what you are trying to do. Look at the differences between our code, there were a few problems with your code. Also, note that I'm not using shinysky because it doesn't have the colourpicker anymore (it's moved to a different package that's inactive), so instead I'm using the inputColour from shinyjs.

library(shiny)
library(shinyjs)

dat <- data.frame(matrix(rnorm(120, 2, 3), ncol=3))

runApp(shinyApp(
  ui = fluidPage(
    uiOutput('myPanel'),
    plotOutput("plot")
  ),
  server = function(input, output, session) {
    cols <- reactive({
      lapply(seq_along(dat), function(i) {
        colourInput(paste("col", i, sep="_"), "Choose colour:", "black")        
      })
    })

    output$myPanel <- renderUI({cols()})

    # Put all the input in a vector
    colors <- reactive({
      lapply(seq_along(dat), function(i) {
        input[[paste("col", i, sep="_")]]
      })
    })

    output$plot <- renderPlot({
      if (is.null(input$col_1)) {
        cols <- rep("#000000", ncol(dat))
      } else {
        cols <- unlist(colors())
      }
      plot(dat[,1], col = cols[1])
      for(i in 2:ncol(dat)) lines(dat[,i], col = cols[i])
    })
  }
))

免责声明:我是shinyjs

这篇关于闪亮的服务器中的动态颜色输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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