Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 891
AnjaM Avatar asked Jul 02 '14 11:07

AnjaM


2 Answers

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

like image 181
jdharrison Avatar answered Nov 02 '22 17:11

jdharrison


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.

like image 25
DeanAttali Avatar answered Nov 02 '22 17:11

DeanAttali