Dynamic color input on a brilliant server

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

What I want to do:

  • Play the colorpicker as many times as the number of variables in the data
  • Take the input from the color and pass it as a color argument in the graph

I am very new to both brilliant 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])
    })

}
))
+4
source share
1 answer

Here is a working version of what you are trying to do. Look at the differences between our code, there were several problems with your code. Also note that I am not using shinyskyit because it no longer has color (it has moved to another package that is inactive), so instead I use inputColourfrom 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 am the author shinyjs

+2
source

All Articles