Title: | A Colour Picker Tool for Shiny and for Selecting Colours in Plots |
---|---|
Description: | A colour picker that can be used as an input in 'Shiny' apps or Rmarkdown documents. The colour picker supports alpha opacity, custom colour palettes, and many more options. A Plot Colour Helper tool is available as an 'RStudio' Addin, which helps you pick colours to use in your plots. A more generic Colour Picker 'RStudio' Addin is also provided to let you select colours to use in your R code. |
Authors: | Dean Attali [aut, cre] , David Griswold [ctb] |
Maintainer: | Dean Attali <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.3.0 |
Built: | 2024-11-16 05:35:56 UTC |
Source: | https://github.com/daattali/colourpicker |
Create an input control to select a colour.
colourInput( inputId, label, value = "white", showColour = c("both", "text", "background"), palette = c("square", "limited"), allowedCols = NULL, allowTransparent = FALSE, returnName = FALSE, closeOnClick = FALSE, width = NULL )
colourInput( inputId, label, value = "white", showColour = c("both", "text", "background"), palette = c("square", "limited"), allowedCols = NULL, allowTransparent = FALSE, returnName = FALSE, closeOnClick = FALSE, width = NULL )
inputId |
The |
label |
Display label for the control, or ' |
value |
Initial value (can be a colour name or HEX code) |
showColour |
Whether to show the chosen colour as text inside the input, as the background colour of the input, or both (default). |
palette |
The type of colour palette to allow the user to select colours
from. |
allowedCols |
A list of colours that the user can choose from. Only
applicable when |
allowTransparent |
If |
returnName |
If |
closeOnClick |
If |
width |
The width of the input, e.g. |
A colour input allows users to select a colour by clicking on the desired
colour, or by entering a valid colour in the input box. Colours can be
specified as either names ("blue"), HEX codes ("#0000FF"), RGB codes
("rgb(0, 0, 255)"), or HSL codes ("hsl(240, 100, 50)"). Use
allowTransparent = TRUE
to allow selecting semi-transparent colours.
The return value is a HEX value by default, but you can use the
returnName = TRUE
parameter to get an R colour name instead
(only when an R colour exists for the selected colour).
When allowTransparent = TRUE
, the user can type into the input field
any RGBA value, HSLA value, or 8-digit HEX with alpha channel You can also use
any of these values as the value
argument as the initial value of the
input.
See https://daattali.com/shiny/colourInput/ for a live demo.
updateColourInput
colourPicker
if (interactive()) { # Example 1 library(shiny) shinyApp( ui = fluidPage( colourInput("col", "Choose colour", "red"), plotOutput("plot") ), server = function(input, output, session) { output$plot <- renderPlot({ plot(1:10, col = input$col) }) } ) # Example 2 library(shiny) shinyApp( ui = fluidPage( strong("Selected colour:", textOutput("value", inline = TRUE)), colourInput("col", "Choose colour", "red"), h3("Update colour input"), textInput("text", "New colour: (colour name or HEX value)"), selectInput("showColour", "Show colour", c("both", "text", "background")), selectInput("palette", "Colour palette", c("square", "limited")), checkboxInput("allowTransparent", "Allow transparent", FALSE), checkboxInput("returnName", "Return R colour name", FALSE), actionButton("btn", "Update") ), server = function(input, output, session) { observeEvent(input$btn, { updateColourInput(session, "col", value = input$text, showColour = input$showColour, allowTransparent = input$allowTransparent, palette = input$palette, returnName = input$returnName) }) output$value <- renderText(input$col) } ) }
if (interactive()) { # Example 1 library(shiny) shinyApp( ui = fluidPage( colourInput("col", "Choose colour", "red"), plotOutput("plot") ), server = function(input, output, session) { output$plot <- renderPlot({ plot(1:10, col = input$col) }) } ) # Example 2 library(shiny) shinyApp( ui = fluidPage( strong("Selected colour:", textOutput("value", inline = TRUE)), colourInput("col", "Choose colour", "red"), h3("Update colour input"), textInput("text", "New colour: (colour name or HEX value)"), selectInput("showColour", "Show colour", c("both", "text", "background")), selectInput("palette", "Colour palette", c("square", "limited")), checkboxInput("allowTransparent", "Allow transparent", FALSE), checkboxInput("returnName", "Return R colour name", FALSE), actionButton("btn", "Update") ), server = function(input, output, session) { observeEvent(input$btn, { updateColourInput(session, "col", value = input$text, showColour = input$showColour, allowTransparent = input$allowTransparent, palette = input$palette, returnName = input$returnName) }) output$value <- renderText(input$col) } ) }
This gadget lets you choose colours easily. You can select multiple colours, and you can either choose any RGB colour, or browse through R colours.
colourPicker(numCols = 3)
colourPicker(numCols = 3)
numCols |
The number of colours to select when the gadget launches (you can add and remove more colours from the app itself too) |
Vector of selected colours
This gadget returns a vector of colours that can be assigned to a variable. If instead you want to get a text representation of the colours that can embedded into code, use the addin from the RStudio Addins menu.
if (interactive()) { cols <- colourPicker(5) }
if (interactive()) { cols <- colourPicker(5) }
Create a colour picker htmlwidget. This is not terribly useful right now
since you can use the more powerful colourInput
in Shiny apps and Rmarkdown documents, but this gives you an htmlwidget
version of that colour picker.
colourWidget( value = "white", showColour = c("both", "text", "background"), palette = c("square", "limited"), allowedCols = NULL, allowTransparent = FALSE, returnName = FALSE, closeOnClick = FALSE, width = "300px", height = "35px", elementId = NULL )
colourWidget( value = "white", showColour = c("both", "text", "background"), palette = c("square", "limited"), allowedCols = NULL, allowTransparent = FALSE, returnName = FALSE, closeOnClick = FALSE, width = "300px", height = "35px", elementId = NULL )
value |
Initial value (can be a colour name or HEX code) |
showColour |
Whether to show the chosen colour as text inside the input, as the background colour of the input, or both (default). |
palette |
The type of colour palette to allow the user to select colours
from. |
allowedCols |
A list of colours that the user can choose from. Only
applicable when |
allowTransparent |
If |
returnName |
If |
closeOnClick |
If |
width |
Custom width for the input field. |
height |
Custom height for the input field. |
elementId |
Use an explicit element ID for the widget (rather than an automatically generated one). |
colourWidget() colourWidget("red", palette = "limited", allowedCols = c("yellow", "red", "#123ABC"))
colourWidget() colourWidget("red", palette = "limited", allowedCols = c("yellow", "red", "#123ABC"))
Allows you to interactively pick combinations of colours, to help you
choose colours to use in your plots. The plot updates in real-time as you
pick colours.
If you often find yourself spending a lot of time re-creating
the same plot over and over with different colours to try to find the best
colours, then the Plot Colour Helper can help you immensely.
Important: The colours you pick will be available as a variable
called CPCOLS
, so you can use CPCOLS
in your plot code. See the
example below.
plotHelper(code = "", colours = NULL, returnCode = FALSE)
plotHelper(code = "", colours = NULL, returnCode = FALSE)
code |
Code for a plot. You can use the variable |
colours |
A vector of colours to use as the initial colours in the tool,
or an integer. If an integer is provided instead of colours, the tool will load
with that number of colours, and default colours will be used initially.
If you do not provide this parameter, the tool will attempt to guess how many
colours are needed in the |
returnCode |
If |
There are many keyboard shortcuts to help you be more efficient. For example, pressing spacebar adds a new colour, left/right keys let you navigate between the selected colours, 1-9 let you select any of the first 9 colours. For a full list of keyboard shortcuts, click on Show keyboard shortcuts.
When this function is called using plotHelper()
, the chosen
colours are returned as a vector of colours. When this is run as an RStudio
addin (through the Addins menu), the resulting code that includes the
colour vector gets inserted into the R document. As a side effect,
CPCOLS
gets assigned in the global environment to the value of the
selected colours.
if (interactive()) { cols <- plotHelper() cols <- plotHelper(colours = c("red", "blue")) cols <- plotHelper(colours = 5) library(ggplot2) cols <- plotHelper(ggplot(mtcars, aes(mpg,wt)) + geom_point(aes(col = as.factor(cyl)))+ scale_colour_manual(values = CPCOLS)) }
if (interactive()) { cols <- plotHelper() cols <- plotHelper(colours = c("red", "blue")) cols <- plotHelper(colours = 5) library(ggplot2) cols <- plotHelper(ggplot(mtcars, aes(mpg,wt)) + geom_point(aes(col = as.factor(cyl)))+ scale_colour_manual(values = CPCOLS)) }
Launch a colourpicker
example Shiny app that shows how to use the
colourInput
control.
The example is also
available online.
runExample()
runExample()
## Only run this example in interactive R sessions if (interactive()) { runExample() }
## Only run this example in interactive R sessions if (interactive()) { runExample() }
Change the value of a colour input on the client.
updateColourInput( session, inputId, label = NULL, value = NULL, showColour = NULL, palette = NULL, allowedCols = NULL, allowTransparent = NULL, returnName = NULL, closeOnClick = NULL )
updateColourInput( session, inputId, label = NULL, value = NULL, showColour = NULL, palette = NULL, allowedCols = NULL, allowTransparent = NULL, returnName = NULL, closeOnClick = NULL )
session |
The |
inputId |
The id of the colour input object. |
label |
The label to set for the input object. |
value |
The value to set for the input object. |
showColour |
Whether to show the chosen colour as text inside the input, as the background colour of the input, or both (default). |
palette |
The type of colour palette to allow the user to select colours
from. |
allowedCols |
A list of colours that the user can choose from. Only
applicable when |
allowTransparent |
If |
returnName |
If |
closeOnClick |
If |
The update function sends a message to the client, telling it to change
the settings of a colour input object.
This function works similarly to the update functions provided by shiny.
Any argument with NULL
values will be ignored.
See https://daattali.com/shiny/colourInput/ for a live demo.
if (interactive()) { library(shiny) shinyApp( ui = fluidPage( div("Selected colour:", textOutput("value", inline = TRUE)), colourInput("col", "Choose colour", "red"), h3("Update colour input"), textInput("text", "New colour: (colour name or HEX value)"), selectInput("showColour", "Show colour", c("both", "text", "background")), checkboxInput("allowTransparent", "Allow transparent", FALSE), checkboxInput("returnName", "Return R colour name", FALSE), actionButton("btn", "Update") ), server = function(input, output, session) { observeEvent(input$btn, { updateColourInput(session, "col", value = input$text, showColour = input$showColour, allowTransparent = input$allowTransparent, returnName = input$returnName) }) output$value <- renderText(input$col) } ) }
if (interactive()) { library(shiny) shinyApp( ui = fluidPage( div("Selected colour:", textOutput("value", inline = TRUE)), colourInput("col", "Choose colour", "red"), h3("Update colour input"), textInput("text", "New colour: (colour name or HEX value)"), selectInput("showColour", "Show colour", c("both", "text", "background")), checkboxInput("allowTransparent", "Allow transparent", FALSE), checkboxInput("returnName", "Return R colour name", FALSE), actionButton("btn", "Update") ), server = function(input, output, session) { observeEvent(input$btn, { updateColourInput(session, "col", value = input$text, showColour = input$showColour, allowTransparent = input$allowTransparent, returnName = input$returnName) }) output$value <- renderText(input$col) } ) }