• Stars
    star
    136
  • Rank 265,654 (Top 6 %)
  • Language
    R
  • License
    Other
  • Created almost 4 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Highlighting R output in the console

emphatic

R-CMD-check

{emphatic} augments the output of data.frames, matrices and simple vectors in R by adding user-defined ANSI highlighting.

See the online documentation for vignettes and more examples.

What’s in the box

There are separate high-level functions for highlighting data.frames, matrices and simple vectors. There is also a low-level highlghting function which can be used on data.frames and matrices.

The hl_ prefix can be read as highlight.

  • hl() for highlighting data.frames
  • hl_mat() for highlighting matrices
  • hl_vec() for highlighting simple atomic vectors
  • hl_loc() for low-level control of highlighting of both data.frames and matrices
  • hl_opt() to set some local options on the current emphatic object e.g. full_colour option sets 24-bit colour mode.
  • hl_opt_global() sets global options for highlighting. These values will be the default unless overridden with a call to hl_opt() for the given emphatic object.
  • hl_str_diff() for highlighting string differences.
  • General highlighting for regular expressions.
    • hl_grep(x, pattern) where x is a character string.
    • hl_grep_character() where the general object x is converted to a string using as.character().
    • hl_grep_print() as above, but converstion to character via print()
    • hl_grep_deparse() as above, but using deparse1() to create string representation of object.
    • hl_grep_str() as above, but using str() to create string representation of object.
data.frame matrix vector
High Level hl() hl_mat() hl_vec()
Low Level hl_loc() hl_loc() NA

Installation

You can install from GitHub with:

# install.package('remotes')
remotes::install_github('coolbutuseless/emphatic', ref = 'main')

Warning

  • This package calls eval() on user-supplied code and extreme caution should be taken before exposing functions in this package to the internet (e.g. via shiny)

Vignettes

Intro

Specifying rows, columns and colours

Test cases on Real data

Advanced:

Example: Highlighting a data.frame with alternating row colours

library(emphatic)
emphatic::hl_opt_global(dark_mode = FALSE)

mtcars |>
  hl(c('red', 'white')) 

Example of highlighting a data.frame and include a legend

Use {emphatic} to highlight the mtcars dataset where:

  • colour each row to indicate the miles-per-gallon rating
  • do not colour the gear or carb columns
  • highlight the car with the maximum miles per gallon in hotpink
mtcars |>
  hl(ggplot2::scale_colour_viridis_c(),
     cols = mpg, dest_cols = mpg:am, show_legend = TRUE) |>
  hl('hotpink', rows = mpg == max(mpg)) |>
  hl_opt(text_contrast = 0.25)

Example: Highlighting a data.frame with rainbows!

mtcars |> 
  hl(rainbow(32)) |>
  hl_opt(text_contrast = 0.5)

Example: Highlighting a matrix - Correlation matrix

Create a correlation matrix of some of the variables in mtcars.

Colour the values using red for negative correlations and blue for positive correlations. Values in-between are coloured using a gradient between red and blue. This colouring is applied using ggplot2::scale_colour_gradient2().

mtcars |>
  select(cyl, mpg, hp, disp, vs) |>
  cor() |>
  hl_mat(scale_colour_gradient2(), selection = abs(.x) > 0.7 & row(.x) != col(.x)) 

Example: Highlighting a numeric vector

Highlight locations in a numeric vector which match an expression.

sample(10, 30, replace = TRUE, prob = 1:10) |>
  hl_vec('green', .x < 3) |>
  hl_vec('blue', .x > 7)

Example: Highlighting the difference between strings

x <- 'hell there!'
y <- 'hello there?'
hl_str_diff(x, y)

x <- 'hello there?'
y <- 'hell there!'
hl_str_diff(x, y)

x <- "Paris in the the spring?"
y <- "Not Paris in the spring!"
hl_str_diff(x, y)

Highlighting grep() matches in character representations of objects

Example: Highlight a string in a data.frame

mtcars |> 
  head(20) |>
  hl_grep_print("Merc")

Highlight a row in a data.frame which matches a word

mtcars |> 
  head(20) |>
  hl_grep_print("(?m)^.*wood.*?$", fg = 'blue', bg = 'hotpink')

Highlight text in a string

string <- 
"<xml>
   <this is='not'>a real XML doc</this>
   <this is='not'>a real HTML doc</this>
   <this is='not'>a real XML doc</this>
   <this is='not'>a real XML doc</this>
</xml>"

hl_grep(string, "html", ignore.case = TRUE)

Highlighting within a character vector

vals <- c('hello', 'there', '#rstats', 'on', 'mastodon')

hl_grep(vals, "rstats")

Highlighting within a numeric vector

hl_grep_character(pi, "589")

  • crayon Colored terminal output on terminals that support ‘ANSI’ color and highlight codes. It also works in ‘Emacs’ ‘ESS’. ‘ANSI’ color support is automatically detected.
  • fansi Counterparts to R string manipulation functions that account for the effects of ANSI text formatting control sequences.

Acknowledgements

  • R Core for developing and maintaining the language.
  • CRAN maintainers, for patiently shepherding packages onto CRAN and maintaining the repository

More Repositories

1

dplyr-cli

Manipulate CSV files on the command line using dplyr
R
269
star
2

ggrgl

3d extension to ggplot
R
181
star
3

ggsvg

Use SVG images as ggplot points
R
136
star
4

tickle

Easily create UIs in base R
R
123
star
5

yyjsonr

Fast JSON package for R
C
113
star
6

devout

Write R graphics output devices in plain R
C++
97
star
7

anotherworld

AnotherWorld ported to R
R
74
star
8

ggreverse

Reverse a ggplot object back into code
R
67
star
9

eventloop

Event Loop in R
R
63
star
10

isocubes

R
61
star
11

wordle

Wordle helper for RStats
R
59
star
12

devoutsvg

Bespoke SVG graphics output device with pattern fill support
R
57
star
13

nara

nativeRaster tools for R
C
57
star
14

rllama

Minimal R wrapper for llama.cpp
C
53
star
15

svgparser

Render SVG images in R. Load SVG data as data.frames
R
46
star
16

ggthreed

3d geoms and stats for ggplot
R
46
star
17

threed

Three-Dimensional Object Transformations
R
43
star
18

minipdf

Minimal pure-R PDF document creator
R
41
star
19

gluestick

Simple, single-function string interpolation in Base R
R
41
star
20

carelesswhisper

Automatic speech recognition in R using whisper.cpp
C
40
star
21

tr808r

TR-808 Drum Machine for R
R
40
star
22

xxhashlite

Very fast hash functions using xxHash
C++
35
star
23

geomlime

ggplot geom_lime()
R
34
star
24

facetious

Home to some alternate facetting for ggplot2
R
34
star
25

ggblur

Blurry Point Geom for ggplot2
R
34
star
26

fugly

Extract named substrings using named capture groups in regular expressions.
R
33
star
27

rbytecode

R bytecode assembler/disassembler
R
32
star
28

minicss

Build CSS selectors, styles and stylesheets within R
R
31
star
29

minisvg

Create SVG documents with R
R
29
star
30

zstdlite

Fast, configurable in-memory compression of R objects with zstd
C
26
star
31

numberwang

numbers to words and vice versa
R
25
star
32

pacman

Pacman-ish game in R
R
24
star
33

memoisetools

a collection of additional caches and helper functions to work alongside the memoise package
R
23
star
34

wingspan

Data about the birds and bonus cards in the boardgame "Wingspan"
R
23
star
35

terse

Terse output for nested lists and data.frames
R
22
star
36

ransid

Convert images to ANSI with R
R
21
star
37

lz4lite

Very Fast compression/decompression of in-memory numeric vectors with LZ4
C
20
star
38

miranda

Fast PRNGs for R
C
20
star
39

hershey

Hershey vector font data for RStats
R
20
star
40

simplercpp

A demo R package incorporating C code with Rcpp
C++
20
star
41

chipmunkcore

R wrapper around the Chipmunk2d physics simulation library
C
19
star
42

minihtml

A package for building HTML documents in R (shiny compatible)
R
18
star
43

foist

Fast Output of Images
C++
17
star
44

simplecall

A demo R package incorporating C code which is called with .Call()
C
17
star
45

minidrawio

Create simple draw.io documents from R
R
16
star
46

displease

Non-linear numeric interpolation
R
16
star
47

CP1919

Cambridge Pulsar at 19 hours and 19 minutes right ascension
R
16
star
48

purler

Fast run-length encoding with NA support and results as a data.frame
C
16
star
49

RStudioConf-2022

16
star
50

CRAN-checks

Notes about extra CRAN checks
16
star
51

nonogram

Nonogram solver in rstats
R
15
star
52

optout

Optimized Graphics Output
R
15
star
53

ggqr

ggplot2 geom for QR codes
R
15
star
54

gridfont

A version of the 'gridfont' vector font in an R friendly format
R
15
star
55

phon

Tools and Data for the CMU Pronouncing Dictionary
R
14
star
56

rd2list

Convert Rd documentation to a structured, human-readable list
R
14
star
57

flagon

Flags of the World
R
14
star
58

chipmunkbasic

Higher-level R6 wrapper around Chipmunk2d rigid body physics simulation library
R
14
star
59

anon

Anonymous function creation in R and purrr
R
14
star
60

devoutdrawio

R graphics device to output to draw.io XML vector format
R
14
star
61

miniverse

A constellation of packages for document creation in R
R
14
star
62

devoutaudio

An R graphics device which renders to audio
R
13
star
63

image2xlsx

Convert an image to an excel spreadsheet
R
13
star
64

insitu

In-place modification of vectors
C
13
star
65

strictlyr

Stricter subset of dplyr
R
13
star
66

svgpatternusgs

SVG patterns from the U.S. Geological Survey for use within R
R
13
star
67

cstructr

Exposing C structrs to R
C
13
star
68

ingrid

Tools for interactive grid creation and manipulation in the console
R
13
star
69

ggecho

ggplot2 stat for echoing data
R
13
star
70

arcadefont

Oldschool arcade vector font
R
13
star
71

devoutrgl

R graphics device to render to {rgl}
R
13
star
72

rconnection

Writing a custom connection for R
C
13
star
73

cgrep

Highlighted grep of R objects
R
12
star
74

serializer

Example showing how to access R's serialization functions from C
C
12
star
75

getrect

Partition matrix into single-valued rectangular areas
R
12
star
76

rmonocypher

Easy to use encryption tools for R
C
12
star
77

visvalingam

R package for Visvalingam Line Simplification
C
11
star
78

smallfactor

An R factor backed by a raw vector rather than an integer vector.
R
11
star
79

analemmatic

Create analemmatic sundials with R
R
11
star
80

fastpng

Read/write 8-bit/16-bit PNGs with rasters, native rasters, numeric+integer arrays, indexed images with palette, packed pixels in raw vector. Configurable compression settings allow for speed/size tradeoff.
C
11
star
81

cairocore

Low-level bindings to the CairoGraphics library for fast 2d drawing operations
R
11
star
82

bdftools

Bitmap font tools for R
R
11
star
83

simplefortran

Demo of how Fortran code could be included in an R package
C
10
star
84

svgpatternsimple

Create some simple repeating SVG patterns in R
R
10
star
85

snowcrash

Encode arbitrary objects as PNGs, rasters and rasterGrobs
R
9
star
86

callme

Compile and load C code for use with .Call()
R
9
star
87

frak

Fractal Generator
C
9
star
88

grrr

Modify function default arguments
R
9
star
89

triangular

Decompose complex polygons into sets of triangles
R
9
star
90

cryogenic

Freezing calls, modifying arguments and evaluating later
R
8
star
91

btnvips

libvips cli wrapper for #RStats (btn = better than nothing)
R
8
star
92

colourlookup

Technical demonstration of hash lookup for R colour names
C
8
star
93

ggdebug

Package to help debug and inspect ggplot stats
R
8
star
94

simplec

Demo R package with C code which is called using ".C()'
C
8
star
95

poissoned

Poisson Disc Sampling in R
R
8
star
96

devoutpdf

A hand-crafted PDF graphics output device written in plain R
R
8
star
97

moggy

R
8
star
98

codespacer

Setup CodeSpace with RStudio support
Dockerfile
8
star
99

flexo

Simple lex/parse tools in R
R
7
star
100

cssparser

Parse CSS into R and apply to HTML
R
7
star