Highlight text within an R object which matches a given regex. This only works in a terminal which supports ANSI colour codes.

There are slightly different versions of the highlighting function depending upon which text version of the object you'd like to match against:

hl_grep(
  x,
  pattern,
  coerce = "default",
  opts = hl_opts(),
  fill = NULL,
  text = NULL,
  ...,
  perl = TRUE
)

Arguments

x

character string

pattern

regular expression string. Note: don't get too fancy here

coerce

How should non-character arguments be coerced to character strings?

default

- the given object x must already be a character string

character

- performs the matching after first calling as.character(x)

print

- performs the matching against the default print(x) output

deparse

- performs the matching after first calling deparse1(x)

str

- performs the matching on the output of calling str(x)

opts

create options list

fill

solid colour for background. If NULL (the default), then the default colour will be selected

text

text colour. If NULL (the default), then a colour will be seleted which contrasts with the fill colour.

...

extra args passed to gsub

perl

logical. use perl style regex. default: TRUE

Examples

hl_grep(mode, 'switch')
#> function (x) 
#> {
#>     if (is.expression(x)) 
#>         return("expression")
#>     if (is.call(x)) 
#>         return(switch(deparse(x[[1L]])[1L], `(` = "(", "call"))
#>     if (is.name(x)) 
#>         "name"
#>     else switch(tx <- typeof(x), double = , integer = "numeric", 
#>         closure = , builtin = , special = "function", tx)
#> }
#> <bytecode: 0x12b102948>
#> <environment: namespace:base>