Package 'signs'

Title: Insert Proper Minus Signs
Description: Provides convenience functions to replace hyphen-minuses (ASCII 45) with proper minus signs (Unicode character 2212). The true minus matches the plus symbol in width, line thickness, and height above the baseline. It was designed for mathematics, looks better in presentation, and is understood properly by screen readers.
Authors: Benjamin E. Wolfe [aut, cre]
Maintainer: Benjamin E. Wolfe <[email protected]>
License: MIT + file LICENSE
Version: 0.1.2.9000
Built: 2024-11-20 03:19:53 UTC
Source: https://github.com/benjaminwolfe/signs

Help Index


Add proper minus signs

Description

The true minus sign (Unicode 2212) – neither an em dash, nor an en dash, nor the usual hyphen-minus – is highly underrated. It makes everything look better!

Usage

signs(
  x,
  ...,
  format = getOption("signs.format", scales::number),
  add_plusses = getOption("signs.add.plusses", FALSE),
  trim_leading_zeros = getOption("signs.trim.leading.zeros", FALSE),
  label_at_zero = getOption("signs.label.at.zero", "none")
)

Arguments

x

Numeric vector.

...

Other arguments passed on to format.

format

Any function that takes a numeric vector and returns a character vector, such as scales::number, scales::comma, or scales::percent (all of which are documented at number_format).

add_plusses

Logical. Should positive values have plus signs?

trim_leading_zeros

Logical. Should signs trim leading zeros from values of x between -1 and 1?

label_at_zero

Character. What should be returned when x = 0? Options "none" (no change), "blank" (a zero-length string), or "symbol" (add a plus-minus symbol).

Details

add_plusses, trim_leading_zeros, and label_at_zero are offered for convenience.

The options signs.format, signs.add.plusses, signs.trim.leading.zeros, and signs.label.at.zero are set when the package is loaded to scales::number, FALSE, FALSE, and "none", respectively. If the package is not loaded and the these options are not otherwise set, signs will use those defaults.

label_at_zero is applied after format; that is, if it is "blank" and you've specified an accuracy of 0.1, -0.04 will show as blank.

Value

A UTF-8 character vector

Examples

x <- seq(-5, 5)
scales::number(x)
signs(x)
signs(x, accuracy = 1, scale = 1, format = scales::percent)
signs(x, add_plusses = TRUE)
signs(x, add_plusses = TRUE, label_at_zero = "blank")
signs(x, add_plusses = TRUE, label_at_zero = "symbol")
signs(x, accuracy = .1, scale = .1, trim_leading_zeros = TRUE)

A function factory to add proper minus signs

Description

Returns a function that will format numeric vectors with proper minus signs.

Usage

signs_format(
  ...,
  format = getOption("signs.format", scales::number),
  add_plusses = getOption("signs.add.plusses", FALSE),
  trim_leading_zeros = getOption("signs.trim.leading.zeros", FALSE),
  label_at_zero = getOption("signs.label.at.zero", "none")
)

Arguments

...

Other arguments passed on to format.

format

Any function that takes a numeric vector and returns a character vector, such as scales::number, scales::comma, or scales::percent (all of which are documented at number_format).

add_plusses

Logical. Should positive values have plus signs?

trim_leading_zeros

Logical. Should signs trim leading zeros from values of x between -1 and 1?

label_at_zero

Character. What should be returned when x = 0? Options "none" (no change), "blank" (a zero-length string), or "symbol" (add a plus-minus symbol).

Details

See signs for details.

Value

A function that takes a numeric vector and returns a UTF-8 character vector

Examples

x <- seq(-5, 5)
scales::number(x)

f1 <- signs_format()
f1(x)

f2 <- signs_format(accuracy = 1, scale = 1, format = scales::percent)
f2(x)

f3 <- signs_format(add_plusses = TRUE)
f3(x)

f4 <- signs_format(add_plusses = TRUE, label_at_zero = "blank")
f4(x)

f5 <- signs_format(add_plusses = TRUE, label_at_zero = "symbol")
f5(x)

f6 <- signs_format(accuracy = .1, scale = .1, trim_leading_zeros = TRUE)
f6(x)