Learn R Programming

unikn (version 0.4.0)

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

Run this code
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")

Run the code above in your browser using DataLab