hl | R Documentation |
Highlight elements in a data.frame by specifying rows and columns, and the
colour to be applied. The colour can be either a vector of colours expressed
as characters (e.g. 'red', '#ff0000'), or a ggplot2
Scale object
e.g. scale_colour_viridis_c()
.
hl(
.data,
palette,
rows = NULL,
cols = NULL,
scale_apply,
elem = "fill",
show_legend = FALSE,
opts = hl_opts()
)
.data |
|
palette |
colours to use for highlighting. This may be a single R colour,
a vector of R colours, or
a |
rows , cols |
specification for rows and columns to target. Default is NULL
for both rows and columns, which will target all columns/rows.
When |
scale_apply |
Only valid when palette is a |
elem |
Apply the highlighting to the 'fill' (the background) or the 'text'. Default: 'fill' |
show_legend |
if a scale object is used for colour, and |
opts |
create options list |
An emphatic object suitable to output to console (for example)
Specifying rows and columns can be done in a number of ways. These methods
are similar to the ideas of tidyselect
and dplyr
commands such
as filter()
and select()
row or column indices specified as a numeric vector
e.g. c(1, 2, 8)
vector of names matching row or column names
e.g. c('mpg', 'wt')
vector of symbols which will be evaluated as
column names e.g. c(mpg, wt)
range of indices specified using the :
operator
e.g. 1:8
range of columns specified using the :
operator
e.g. mpg:wt
starts_with()
, ends_with()
,
everything()
, all_of()
, any_of()
, matches()
contains()
, row_number()
, n()
.
These work similar to dplyr
and tidyselect
but are bespoke
implementations so there may be some differences
specifying NULL
means that all rows/columns will be
selected
specifying all()
means that all rows/columns will be
selected
For row selection only, the user
can specify code which will evaluate to a logical vector of rows which
the highlighting should apply to. These will look like statements used
in dplyr::filter()
. E.g. cyl == 6 & mpg > 20
# Simple
mtcars |>
head() |>
hl(c('red', 'blue'))
# More involved example
mtcars |>
head() |>
hl(
ggplot2::scale_colour_viridis_c(),
rows = cyl == 6,
cols = mpg,
scale_apply = c(mpg, cyl)
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.