Dynamic color input in shiny server

2.8k views Asked by At

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.

What I want to do is:

  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

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])
    })

}
))
1

There are 1 answers

2
DeanAttali On BEST ANSWER

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])
    })
  }
))

Disclaimer: I'm the author of shinyjs