AnjaM
AnjaM

Reputation: 3039

Reactive colours in shiny

Is it possible to use a selection widget for displaying a colour palette for reactive colour picking? I'd like to let the user pick the colour(s) for the plot that is created by a shiny app.

Upvotes: 6

Views: 1139

Answers (2)

DeanAttali
DeanAttali

Reputation: 26323

For anyone coming here looking for a colour picker, the previous answer using shinysky is outdated (the colour picker from there has been moved to a package that is not under maintenance)

There is another colour picker available for shiny in the shinyjs package.

library(ggplot2)
library(shiny)
library(shinyjs)

runApp(shinyApp(
  ui = fluidPage(
    colourInput("col", "Select colour", "grey"),
    plotOutput("plot")
  ),
  server = function(input, output, session) {
    output$plot <- renderPlot({
      ggplot(cars, aes(speed, dist)) +
        geom_point() +
        theme(panel.background = element_rect(fill = input$col))
    })
  }
))

enter image description here

Disclaimer: I'm the author of this package.

Upvotes: 3

jdharrison
jdharrison

Reputation: 30425

The shinysky package has a colorpicker which you can use with shiny:

require(shinysky)
require(shiny)

runApp(list(
  ui = bootstrapPage(
    jscolorInput("colorid"), 
    uiOutput('myPanel'),
    plotOutput('plot')
  ),
  server = function(input, output) {
    output$myPanel <- renderUI({
      mystyle <- ifelse(is.null(input$colorid), "ffffff", input$colorid)
      inputPanel(
        numericInput('n', 'Number of obs', 100)
        , style = paste0("background-color:#", mystyle, ";")
      )
    })
    output$plot <- renderPlot({ hist(runif(input$n)) })
  }
))

enter image description here

It is currently not on CRAN so you will need to install it via devtools details are at https://github.com/AnalytixWare/ShinySky

Upvotes: 6

Related Questions