admin管理员组

文章数量:1425862

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.

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.

Share Improve this question edited Jul 2, 2014 at 11:42 Spacedman 94.5k12 gold badges147 silver badges230 bronze badges asked Jul 2, 2014 at 11:40 AnjaMAnjaM 3,04910 gold badges41 silver badges65 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

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

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

For anyone ing 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))
    })
  }
))

Disclaimer: I'm the author of this package.

本文标签: javascriptReactive colours in shinyStack Overflow