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(
uiOutput('myPanel'),
plotOutput('plot')
),
server = function(input, output) {
cols <- reactive({
n <- ncol(dat)
for(i in 1:n){
print(jscolorInput(paste("col", i, sep="_")))
}
})
output$myPanel <- renderPrint({cols()})
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])
})
}
))
source
share