Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

grepal

Get a vector of colors whose names match a regular expression.


Description

grepal returns a vector of colors whose names match a regular expression (regex).

Usage

grepal(pattern, x = colors(), ignore_case = TRUE)

Arguments

pattern

A regular expression (specified as a string/character object).

x

A vector of R color names or a data frame of named colors (i.e., whose names can be searched). Default: x = colors().

ignore_case

Should the case of pattern be ignored (passed to ignore.case of the grep function)? Default: ignore_case = TRUE.

Details

By default, the base R vector of named colors (i.e., colors()) is searched for names matching a pattern (which can be a simple string or regular expression).

If x (i.e., the object to be searched) is provided, it is must be a vector of color names or a data frame of named color objects (e.g., a color palette).

The name grepal is an abbreviation of grep and "pal".

See Also

defpal to define color palettes; seepal to plot color palettes; usecol to use a color palette.

Other color functions: newpal(), seecol(), usecol()

Examples

grepal("cyan")

# With regular expressions:
some_grey  <- grepal("gr(a|e)y")
start_grey <- grepal("^gr(a|e)y")
only_grey  <- grepal("^gr(a|e)y$")

length(some_grey)
length(only_grey)

# With other color objects (df as x):
grepal("blau", x = pal_unikn)
grepal("SEE", x = pal_unikn_pref)

# Applications:
seecol(grepal("white"), col_bg = "lightblue2", title = "See 'white' colors()")

olives  <- grepal("olive")
oranges <- grepal("orange")
seecol(list(olives, oranges), 
       pal_names = c("olives", "oranges"), 
       title = "Comparing olives and oranges")

seecol(grepal("SEE", pal_unikn), title = "All 'SEE' colors in pal_unikn")
seecol(grepal("blau", pal_unikn_pref), title = "All 'blau' colors in pal_unikn_pref")

unikn

Graphical Elements of the University of Konstanz's Corporate Design

v0.4.0
CC BY-SA 4.0
Authors
Hansjoerg Neth [aut, cre] (<https://orcid.org/0000-0001-5427-3141>), Nico Gradwohl [aut] (<https://orcid.org/0000-0002-8703-905X>)
Initial release
2021-03-25

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.