-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathREADME.Rmd
402 lines (284 loc) · 12.3 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
library(dplyr)
library(ggplot2)
library(emphatic)
set.seed(1)
```
```{r echo = FALSE, eval = FALSE}
# Quick logo generation. Borrowed heavily from Nick Tierney's Syn logo process
library(magick)
library(showtext)
font_add_google("Alfa Slab One", "gf")
if (FALSE) {
pkgdown::build_site(override = list(destination = "../coolbutuseless.github.io/package/emphatic"))
}
```
```{r echo = FALSE, eval = FALSE}
img <- image_read("man/figures/white.png")
hexSticker::sticker(subplot = img,
s_x = 1,
s_y = 1.2,
s_width = 1.5,
s_height = 0.95,
package = "emphatic",
p_x = 1,
p_y = 1.05,
p_color = "#223344",
p_family = "gf",
p_size = 8,
h_size = 1.2,
h_fill = "#ffffff",
h_color = "#223344",
filename = "man/figures/logo.png")
image_read("man/figures/logo.png")
```
# emphatic
<!-- badges: start -->
![](https://img.shields.io/badge/cool-useless-green.svg)
[![R-CMD-check](https://github.com/coolbutuseless/emphatic/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/coolbutuseless/emphatic/actions/workflows/R-CMD-check.yaml)
[![CRAN](https://www.r-pkg.org/badges/version/emphatic)](https://CRAN.R-project.org/package=emphatic)
<!-- badges: end -->
`{emphatic}` is a tool for exploratory analysis of tabular data. It allows the user
to visually colour elements of the data, yet still keep all values visible.
Conceptually, `{emphatic}` highlighting could be considered to lie between *tabular output*
and *graphical output* - like a table it shows all values, but like graphs these
values are used to control appearance (i.e. colour).
`{emphatic}` follows the conventions
of `{dplyr}` for selecting rows and columns; advanced colouring is via
colour scales provided by `{ggplot2}`.
#### Supported output
This highlighting works
* in the **console**
* when output to **Excel** (using `write_xlsx()`)
* rendered in **Rmarkdown** documents (to `HTML`, `latex` and `typst` outputs)
* rendered in **Quarto** documents (to `HTML`, `latex` and `typst` outputs)
* rendered to **SVG**
* rendered as multiple frames to **animated SVG**
<details open>
<summary style="font-size: large;">Click here to show/hide gif demo</summary>
<img src="man/figures/examples.gif"/>
</details>
#### What's in the box
* `hl()` for user-defined highlighting of data.frames
* `hl_diff()` for highlighting differences between two objects
* `hl_grep()` highlight regular expression matches in an object or string
* Manual conversion to other formats using:
* `as_html()`
* `as_svg()` and animated `as_svg_anim()`
* `as_typst()`
* `write_xlsx()` - Excel document
* Conversion to the correct output format is performed automatically when
knitting an Rmarkdown or Quarto document.
## Installation
This package can be installed from CRAN
``` r
install.packages('emphatic')
```
You can install the latest development version from
[GitHub](https://github.com/coolbutuseless/emphatic) with:
``` r
# install.package('remotes')
remotes::install_github('coolbutuseless/emphatic')
```
Pre-built source/binary versions can also be installed from
[R-universe](https://r-universe.dev)
``` r
install.packages('emphatic', repos = c('https://coolbutuseless.r-universe.dev', 'https://cloud.r-project.org'))
```
## Highlighting of data.frames with `hl()`
`hl()` lets you specify a palette, and the rows/columns of the data.frame the
palette should apply to.
`hl()` calls are cumulative - so the required highlighting can be built up one-step-at-a-time.
To add highlighing to a data.frame:
* **Specify a palette**
* a single colour or vector of colours
* a `ggplot2` *Scale* object e.g. `scale_colour_continuous()`
* **Specify rows and columns**
* default: highlight all rows and all columns.
* numeric vector giving row/column indices e.g. `c(1, 2, 8)`, `1:8`
* character vector giving row/column names e.g. `c('mpg', 'wt')`
* vector of bare column names e.g. `c(mpg, wt)`, `mpg:wt`
* tidyselect-style selectors: `starts_with()`, `ends_with()`, `everything()`,
`all_of()`, `any_of()`, `matches()`, `contains()`, `row_number()`, `n()`
* row selection using filtering operations e.g. `cyl == 6 & mpg > 20`
#### Simple example
By default, colouring will be applied to all rows and columns, and the
supplied vector of colours will be recycled to meet the required length.
```{r example, eval = FALSE}
mtcars |>
head(15) |>
hl(c('red', 'white', 'blue'))
```
```{r eval = TRUE, echo = FALSE}
mtcars |>
head(15) |>
hl(c('red', 'white', 'blue')) |>
as_svg(650, 350) |>
cat(file = "man/figures/example1.svg")
```
<img src="man/figures/example1.svg" width="100%">
#### Complex example
A more complex example showing how to highlight the `mtcars` dataset where:
* Highlight the row for the car with the minimum horsepower
* Determine an expressive colouring for `mpg` using `scale_colour_viridis_c()` -
where low values are darker, and high values are a bright yellow
* Apply the scale's colouring to all columns from `mpg` to `disp`
```{r eval = FALSE}
mtcars |>
head(15) |>
hl('hotpink', rows = hp == min(hp)) |>
hl(
palette = ggplot2::scale_colour_viridis_c(option = 'A'),
cols = mpg, # Where the colour scale is calculated
scale_apply = mpg:disp, # Where the colour scale is applied
show_legend = TRUE
)
```
```{r echo = FALSE}
mtcars |>
head(15) |>
hl(
palette = ggplot2::scale_colour_viridis_c(option = 'A'),
cols = mpg, # Where the colour scale is calculated
scale_apply = mpg:disp, # Where the colour scale is applied
show_legend = TRUE
) |>
hl('hotpink', rows = hp == min(hp), cols = hp:carb) |>
as_svg(650, 380) |>
cat(file = "man/figures/example2.svg")
```
<img src="man/figures/example2.svg" width="100%">
## Highlight difference between two objects with `hl_diff()`
The Levenshtein edit distance is calculated between the string representation
of two objects and these edits are then coloured 🟢 = insert, 🔴 = delete,
🔵 = substitute.
```{r eval = FALSE}
x <- "Paris in the the spring?"
y <- "Not Paris in the spring!"
hl_diff(x, y)
```
```{r echo = FALSE}
x <- "Paris in the the spring?"
y <- "Not Paris in the spring!"
hl_diff(x, y) |>
as_svg(600, 80) |>
cat(file = "man/figures/example-strdiff-3.svg")
```
<img src="man/figures/example-strdiff-3.svg" width="100%">
Levenshtein's edit distance naturally applies to strings, but `hl_diff()` can
visualise the difference between arbitrary objects by first converting them
to a string representation. Coercion to a string is controlled by the
`coerce` argument, and defaults to the output if the objects were `print()`ed.
In this example, the difference between the `mean()` and `median()` function
definitions is highlighted.
```{r eval=FALSE}
hl_diff(mean, median, coerce = 'print', sep = " ")
```
```{r echo=FALSE}
hl_diff(mean, median, coerce = 'print', sep = " ") |>
as_svg(600, 200) |>
cat(file = "man/figures/example-strdiff-4.svg")
```
<img src="man/figures/example-strdiff-4.svg" width="100%">
## Highlight regular expression matches in objects with `hl_grep()`
`hl_grep()` highlights the regular expression matches within a string or
objects coerced into a string representation.
#### Highlight regular expression matches in a character string
```{r eval = FALSE}
txt <- "Among the few possessions he left to his heirs was a set of
Encyclopedia Britannica in storage at the Lindbergh Palace Hotel under
the names Ari and Uzi Tenenbaum. No-one spoke at the funeral, and
Father Petersen's leg had not yet mended, but it was agreed among them
that Royal would have found the event to be most satisfactory.
[Chas, now wearing a black Adidas tracksuit, nods to his sons]"
hl_grep(txt, "event.*satisfactory", coerce = 'character')
```
```{r echo = FALSE}
txt <- "Among the few possessions he left to his heirs was a set of
Encyclopedia Britannica in storage at the Lindbergh Palace Hotel under
the names Ari and Uzi Tenenbaum. No-one spoke at the funeral, and
Father Petersen's leg had not yet mended, but it was agreed among them
that Royal would have found the event to be most satisfactory.
[Chas, now wearing a black Adidas tracksuit, nods to his sons]"
hl_grep(txt, "event.*satisfactory", coerce = 'character')|>
as_svg(600, 200) |>
cat(file = "man/figures/example-hlgrep-1.svg")
```
<img src="man/figures/example-hlgrep-1.svg" width="100%">
#### Highlight regular expression matches within an object
Other R objects (functions, lists, data.frames, etc) can also be highlighted
with regular expressions. How an object is coerced into string representation is
controlled by the `coerce` argument.
In this example, the function body for `mode()` is searched for the word `switch`:
```{r eval = FALSE}
hl_grep(mode, 'switch')
```
```{r echo = FALSE}
hl_grep(mode, 'switch') |>
as_svg(600, 250) |>
cat(file = "man/figures/example-hlgrep-2.svg")
```
<img src="man/figures/example-hlgrep-2.svg" width="100%">
# Animated SVG
Multiple *emphatic* objects may be rendered to an svg animation using `as_svg_anim()`
```{r eval = FALSE}
objs <- list(
hl_grep("hello", "there"),
hl_grep("goodbye", "good boy")
)
svg <- as_svg_anim(objs, width = 600, height = 300, duration = 2,
playback = 'infinite', font_size = "2em")
```
```{r echo = FALSE}
objs <- list(
hl_diff("hello", "there"),
hl_diff("goodbye", "good boy")
)
svg <- as_svg_anim(objs, width = 600, height = 100, duration = 2,
playback = 'infinite', font_size = "2em")
writeLines(svg, "man/figures/example-svg-anim.svg")
```
<img src="man/figures/example-svg-anim.svg">
## Options
* `hl_opts()` create a named list of default options accepted by the functions
in this package
* `hl_adjust()` to adjust options after creation.
* These options are initialised at package start time using `Sys.getenv()`.
Set these values as environment variables in your `.Rprofile` to save
your preferred settings across different sessions. e.g.
* `Sys.setenv(HL_NA = "<none>")` prior to loading package or in `.Rprofile`
* `options(HL_NA = ".")` at any time
| Option | Description |
|:-----------|:------------------|
| `HL_NA` | String to use for NA values. Default "NA" |
| `HL_FULL_COLOUR` | Should full colour ANSI codes be used when outputting to the console? Default: FALSE on Rstudio, but TRUE on all other R consoles |
| `HL_TEXT_MODE` | How to handle text if no text colour has been explicitly specified by the user |
| | `"contrast"` (default) automatically select a colour which contrasts with the background |
| | `"asis"` do not change the colour from the console's default |
| | `"remove"` remove all text without a user-defined colour |
| `HL_TEXT_CONTRAST` | When `text_mode = "contrast"` this numeric value (in range [0, 1]) adjusts the visibility of the text. Default: 1 (high contrast) |
| `HL_GREP_COL` |The fill colour to use with `hl_grep()` if no colour is specified. Default: "#0F19F0"|
| `HL_SUB_COL`, `HL_INS_COL`, `HL_DEL_COL` | the default colours to use with `hl_diff()` for substitution, insertion and deletion (respectively). |
## Vignettes
See the [online documentation](https://coolbutuseless.github.io/package/emphatic/index.html) for
vignettes and more examples.
* [Highlighting data.frames](https://coolbutuseless.github.io/package/emphatic/articles/aaa-data-frames.html)
* Specifying rows, columns and colours
* [Specifying rows](https://coolbutuseless.github.io/package/emphatic/articles/specify-rows.html)
* [Specifying columns](https://coolbutuseless.github.io/package/emphatic/articles/specify-columns.html)
* [Specifying colours](https://coolbutuseless.github.io/package/emphatic/articles/specify-colours.html)
* Worked Examples
* [Space Shuttle O-ring dataset - Challenger Disaster](https://coolbutuseless.github.io/package/emphatic/articles/challenger.html)
```{r echo = FALSE}
knitr::knit_exit()
```