Title: | Exploratory Analysis of Tabular Data using Colour Highlighting |
---|---|
Description: | Tools for exploratory analysis of tabular data using colour highlighting. Highlighting is displayed in any console supporting 'ANSI' colours, and can be converted to 'HTML', 'typst', 'latex' and 'SVG'. 'quarto' and 'rmarkdown' rendering are directly supported. It is also possible to add colour to regular expression matches and highlight differences between two arbitrary R objects. |
Authors: | Mike Cheng [aut, cre, cph] |
Maintainer: | Mike Cheng <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.8 |
Built: | 2024-11-01 05:40:20 UTC |
Source: | https://github.com/coolbutuseless/emphatic |
This usually does not need to be called explicitly by the user.
as_emphatic(.data)
as_emphatic(.data)
.data |
data.frame, matrix or atomic vector |
The function adds the attributes necessary for keeping track of the colours assigned to each cell. This consists of 2 character matrices - one for the text colour and one for the background colour.
Colour information is stored as R colour names (e.g. 'red') or 6 character hex colours (e.g. '#ff0000').
An emphatic
version of the given .data with added attributes for text and fill colours
mtcars |> head() |> as_emphatic()
mtcars |> head() |> as_emphatic()
Render an emphatic object to HTML
as_html( x, ..., font_size = NULL, style = list(), complete = FALSE, browsable = FALSE )
as_html( x, ..., font_size = NULL, style = list(), complete = FALSE, browsable = FALSE )
x |
emphatic object |
... |
other arguments passed to |
font_size |
CSS font-size. Default: NULL means to not adjust font size.
Otherwise, use valid CSS |
style |
html tag styling to apply to the |
complete |
logical. Default: FALSE. If TRUE, then add DOCTYPE and the tags for 'html', 'body' and 'head' to make a complete standalone html file. |
browsable |
Should the SVG be rendered to the RStudio Viewer pane when when printed (instead of console output)? Default: FALSE |
Character string containing HTML representation
hl_diff('hello', 'there') |> as_html() |> cat()
hl_diff('hello', 'there') |> as_html() |> cat()
Render an emphatic object to Latex
as_latex(x, ..., font_size = NULL)
as_latex(x, ..., font_size = NULL)
x |
emphatic object |
... |
other arguments passed to |
font_size |
Integer value indicating font size measured in points. Default: NULL. |
single character string containing a latex representation
hl_diff("hello", "there") |> as_latex() |> cat()
hl_diff("hello", "there") |> as_latex() |> cat()
This is mainly useful within a github README.md
since github will
not rendered html-styled text in colour, but will render it correctly
if it is within a <svg>
tags.
as_svg( x, width = 1200, height = 900, ..., font_size = NULL, style = list(), browsable = FALSE )
as_svg( x, width = 1200, height = 900, ..., font_size = NULL, style = list(), browsable = FALSE )
x |
emphatic object |
width , height
|
viewBox dimensions for SVG |
... |
other arguments passed to |
font_size |
CSS font-size. Default: NULL means to not adjust font size.
Otherwise, use valid CSS |
style |
html tag styling to apply to the |
browsable |
Should the SVG be rendered to the RStudio Viewer pane when when printed (instead of console output)? Default: FALSE |
This is just a the results of as_html()
wrapped in <svg>
tags
Character string containing SVG representation
hl_diff('hello', 'there') |> as_svg() |> cat()
hl_diff('hello', 'there') |> as_svg() |> cat()
Idea borrowed from pointblank
as_svg_anim( x, width = 1200, height = 900, duration = 1, playback = c("infinite", "click"), font_size = NULL, style = list(), svg_id = NULL, browsable = FALSE )
as_svg_anim( x, width = 1200, height = 900, duration = 1, playback = c("infinite", "click"), font_size = NULL, style = list(), svg_id = NULL, browsable = FALSE )
x |
list of emphatic objects |
width , height
|
viewBox dimensions for SVG |
duration |
frame duration in seconds. May be a single value used for all frames, or a vector of values (one duration value for each frame). Can be fractions of a second. |
playback |
'click', 'infinite' |
font_size |
CSS font-size. Default: NULL means to not adjust font size.
Otherwise, use valid CSS |
style |
html tag styling to apply to the |
svg_id |
ID to use for the SVG tag. Default: NULL means to create a random ID |
browsable |
Should the SVG be rendered to the RStudio Viewer pane when when printed (instead of console output)? Default: FALSE |
Character string containing an animated SVG representation displaying all elements sequentially
list( hl_diff('hello', 'there'), hl_diff('goodbye', 'good boy') ) |> as_svg_anim() |> cat()
list( hl_diff('hello', 'there'), hl_diff('goodbye', 'good boy') ) |> as_svg_anim() |> cat()
This function wraps html
in SVG group tags (i.e. <g>
). This
may then be wrapped in <svg>
tags to create a stand-along SVG.
as_svg_group( x, width = 1200, height = 900, font_size = NULL, style = list(), visible = TRUE, extra = NULL, ... )
as_svg_group( x, width = 1200, height = 900, font_size = NULL, style = list(), visible = TRUE, extra = NULL, ... )
x |
emphatic object |
width , height
|
viewBox dimensions for SVG |
font_size |
CSS font-size. Default: NULL means to not adjust font size.
Otherwise, use valid CSS |
style |
html tag styling to apply to the |
visible |
should the group be visible? Default: TRUE. When animating,
every frame other than the first should be set as |
extra |
extra tags to insert into group. default NULL |
... |
other arguments passed to |
This function is used internall by both as_svg()
and as_svg_anim()
Character string containing representation as an SVG group element
i.e. <g>
. This result is suitable for combining with other
SVG elements into a custom SVG document.
hl_diff('hello', 'there') |> as_svg_group() |> cat()
hl_diff('hello', 'there') |> as_svg_group() |> cat()
Render an emphatic object to typst
as_typst(x, ..., font_size = 10, font = NA, line_spacing = 0.3)
as_typst(x, ..., font_size = 10, font = NA, line_spacing = 0.3)
x |
emphatic object |
... |
other arguments passed to |
font_size |
font size in points. default: 10 |
font |
name of font. Default: NA means to just use the default raw font |
line_spacing |
line spacing in |
Character string containing typst
representation
hl_diff("hello", "there") |> as_typst() |> cat()
hl_diff("hello", "there") |> as_typst() |> cat()
emphatic
data.frame, matrix or atomic vector into a character string.The output contains ANSI escape codes to colour the elements in the
object. This string would then be suitable to pass on to fansi
for further manipulation e.g. conversion to HTML for displaying in a vignette.
## S3 method for class 'emphatic' as.character(x, ..., mode = "ansi")
## S3 method for class 'emphatic' as.character(x, ..., mode = "ansi")
x |
|
... |
other arguments passed on to |
mode |
Render mode 'ansi' (default) or 'html' determines how the colours will be represented in text. If you're in a terminal or console, then choose 'ansi'. |
A character string of the requested mode
mtcars |> as_emphatic() |> as.character()
mtcars |> as_emphatic() |> as.character()
A dataset containing information about the o-ring status of the flights leading up to the Space Shuttle Challenger distaster.
challenger
challenger
A data.frame
Flight number
Launch temperature (Fahrenheit)
Number of o-ring erosion incidents
Number of o-ring blow-by incidents
Damage severity index
Date of launch
Sourced from a table in Tufte's "Visual and Statistical Thinking"
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() )
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) )
# 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) )
Set options for printing on the emphatic matrix or data.frame
hl_adjust(.data, na, full_colour, text_mode, text_contrast)
hl_adjust(.data, na, full_colour, text_mode, text_contrast)
.data |
emphatic matrix or data.frame |
na |
Character string to display for NA values. Default 'NA' |
full_colour |
Use 24bit ANSI escape codes? default: FALSE - use 8bit colour. Note: RStudio only supports 8 bit ANSI output (24bit ANSI is rendered invisibly in Rstudio). For 24bit colour output, try R in the terminal e.g. 'iTerm' on OSX. |
text_mode |
How to handle text if no text colour has been explicitly specified by the user.
|
text_contrast |
When |
emphatic object with updated options
mtcars |> hl('red') |> hl_adjust(text_contrast = 0.3)
mtcars |> hl('red') |> hl_adjust(text_contrast = 0.3)
Highlight the differences between two strings in
terms of substitutions, insertions and deletions calculated by
the generalized Levenshtein (edit) distance (using adist()
)
hl_diff( x, y, coerce = "default", fill = NULL, text = NULL, opts = hl_opts(), sep = NULL, ... )
hl_diff( x, y, coerce = "default", fill = NULL, text = NULL, opts = hl_opts(), sep = NULL, ... )
x , y
|
each argument is a single string. vectors of strings not currently supported. |
coerce |
How should non-character arguments be coerced to character strings?
|
fill |
named list of colours for substitutions, insertions and deletions with names 'sub', 'ins' and 'del'. If set to NULL (the default) then default colours will be used. |
text |
named list of colours for the text for 'sub', 'ins' and 'del'
operations. If |
opts |
create options list |
sep |
character string of the line separating the two objects. Default: |
... |
further arguments passed to |
This works character-by-character, so the displayed difference for multiline strings can be quite busy if there are a lot of changes.
list of 'emphatic' objects which could be rendered to ANSI (for example)
hl_diff('hello', 'there')
hl_diff('hello', 'there')
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 )
hl_grep( x, pattern, coerce = "default", opts = hl_opts(), fill = NULL, text = NULL, ..., perl = TRUE )
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?
|
opts |
create options list |
fill |
solid colour for background. If |
text |
text colour. If |
... |
extra args passed to |
perl |
logical. use perl style regex. default: TRUE |
An emphatic object suitable to output to console (for example)
hl_grep(mode, 'switch')
hl_grep(mode, 'switch')
Create a set of options
hl_opts( na = getOption("HL_NA", "NA"), full_colour = getOption("HL_FULL_COLOUR", FALSE), text_mode = getOption("HL_TEXT_MODE", "contrast"), text_contrast = getOption("HL_TEXT_CONTRAST", 1) )
hl_opts( na = getOption("HL_NA", "NA"), full_colour = getOption("HL_FULL_COLOUR", FALSE), text_mode = getOption("HL_TEXT_MODE", "contrast"), text_contrast = getOption("HL_TEXT_CONTRAST", 1) )
na |
Character string to display for NA values. Default 'NA' |
full_colour |
Use 24bit ANSI escape codes? default: FALSE - use 8bit colour. Note: RStudio only supports 8 bit ANSI output (24bit ANSI is rendered invisibly in Rstudio). For 24bit colour output, try R in the terminal e.g. 'iTerm' on OSX. |
text_mode |
How to handle text if no text colour has been explicitly specified by the user.
|
text_contrast |
When |
named list of standard options
# Generate a standard set of options hl_opts()
# Generate a standard set of options hl_opts()
Check if data.frame, matrix or atomic vector is a valid emphatic version
is_emphatic(x)
is_emphatic(x)
x |
Object to test |
Logical value
mtcars |> hl('red') |> is_emphatic()
mtcars |> hl('red') |> is_emphatic()
Automatically output emphatic objects to HTML knitted documents.
knit_print.emphatic(x, style = list(), ...)
knit_print.emphatic(x, style = list(), ...)
x |
emphatic object |
style |
html tag styling to apply to the |
... |
other arguments passed to |
a character vector suitable for output during an rmarkdown render
mtcars |> hl('red') |> knit_print.emphatic()
mtcars |> hl('red') |> knit_print.emphatic()
emphatic
data.frame, matrix or atomic vectorPrint an emphatic
data.frame, matrix or atomic vector
## S3 method for class 'emphatic' print(x, ...)
## S3 method for class 'emphatic' print(x, ...)
x |
|
... |
other arguments passed on to |
None.
mtcars |> head() |> hl('red') |> print()
mtcars |> head() |> hl('red') |> print()
From the 'National Snow and Ice Data Center' https://nsidc.org/data/g02135
sea_ice_area
sea_ice_area
Matrix of sea ice area, monthly from 1978 to 2020.
Show HTML or SVG content in the rstudio viewer pane
show_html(x, viewer = getOption("viewer", utils::browseURL))
show_html(x, viewer = getOption("viewer", utils::browseURL))
x |
svg or html |
viewer |
function which activates viewer |
None
# This example will try and spawn an external viewer for HTML content hl_grep(mode, "switch") |> as_html() |> show_html()
# This example will try and spawn an external viewer for HTML content hl_grep(mode, "switch") |> as_html() |> show_html()
From the Australian Bureau of Meteorology
sydney_rain
sydney_rain
data.frame with each row representing a year, and each column representing a month of that year
Requires openxlsx
package
write_xlsx(x, xlsx_filename, colNames = TRUE, opts = hl_opts())
write_xlsx(x, xlsx_filename, colNames = TRUE, opts = hl_opts())
x |
emphatic data.frame object |
xlsx_filename |
xlsx filename |
colNames |
Display column names? logical. Default: TRUE |
opts |
rendering options |
None
mtcars |> hl('blue') |> write_xlsx(tempfile())
mtcars |> hl('blue') |> write_xlsx(tempfile())