justify.proportional: Justify numbers with proportional characters

View source: R/utils_justify.R

justify.proportionalR Documentation

Justify numbers with proportional characters

Description

justify.proportional is a variation on the justify function that, instead of using a single character (e.g. " ") to pad strings to achieve justification, will use characters from other strings in the vector. This is useful when the output font is not monospaced, and may not even use tabular numbers (i.e. the number widths may be proportional). Usually, the decimal character is not the same width as the numbers, and this can create problems when using extra characters to justify a column of numbers. The function returns a list of two vectors: a left-padding vector of string and right-padding vector of strings. Concatenate these with the original vector of strings to achieve the desired justification.

Usage

justify.proportional(
  input_vec,
  justify_mode = "d",
  decimal = ".",
  tabular_numbers = TRUE,
  tabular_char = "0",
  unicode_spaces = FALSE,
  monospace = FALSE
)

Arguments

input_vec

Input vector. Coerced to character.

justify_mode

Mode to use for justifying a vector of values. Can be "l" (left), "c" (center), "r" (right), or "d" (decimal).

decimal

Character to justify around when justify_mode = "decimal"

tabular_numbers

When TRUE (the default), we assume that all digits have the same width in the target font. We can therefore use a single character (specified by tabular_char) to pad all of our strings. Any non-digit characters (i.e. the decimal character) are still treated as if they have a unique widths that need to be accounted for.

By specifying tabular_numbers = FALSE, we assume that each digit in the target font can have varying (proportional) widths. Therefore, in order to guarantee correct justification, the paddings must consist of a common set of characters. The tabular_numbers = FALSE mode finds a common set of characters across all strings in the vector, and then creates paddings from the setdiff of each string with the common set. Each string in the final compiled result (l_pad + string + r_pad) will therefore contain the same set of common characters, thereby ensuring justification.

tabular_char

See tabular_numbers

unicode_spaces

Override to replace paddings with unicode spaces, using punctuation space (U+2008) punctuation and figure space (U+2007) otherwise. Assumes all digits are the same width and supercedes tabular numbers if TRUE.

monospace

Override to assume all characters are monospaced. Supercedes other options if TRUE.


adamleejohnson/R-ajtools documentation built on April 4, 2022, 7:24 a.m.