Learn R Programming

tinytable (version 0.14.0)

format_vector: Format a Vector

Description

Format a Vector

Usage

format_vector(
  x,
  output = "html",
  digits = NULL,
  num_fmt = "significant",
  num_zero = FALSE,
  num_suffix = FALSE,
  num_mark_big = "",
  num_mark_dec = getOption("OutDec", default = "."),
  date = NULL,
  bool = NULL,
  math = FALSE,
  other = NULL,
  replace = FALSE,
  escape = FALSE,
  markdown = FALSE,
  quarto = FALSE,
  fn = NULL,
  sprintf = NULL,
  linebreak = NULL
)

Value

A character vector with formatted values.

Arguments

x

A vector to be formatted.

output

Output format. One of "html", "latex", "typst", "markdown", etc.

digits

Number of significant digits or decimal places.

num_fmt

The format for numeric values; one of 'significant', 'significant_cell', 'decimal', or 'scientific'.

num_zero

Logical; if TRUE, trailing zeros are kept in "decimal" format (but not in "significant" format).

num_suffix

Logical; if TRUE display short numbers with digits significant digits and K (thousands), M (millions), B (billions), or T (trillions) suffixes.

num_mark_big

Character to use as a thousands separator.

num_mark_dec

Decimal mark character. Default is the global option 'OutDec'.

date

A string passed to the format() function, such as "%Y-%m-%d". See the "Details" section in ?strptime

bool

A function to format logical columns. Defaults to title case.

math

Logical. If TRUE, wrap cell values in math mode $..$. This is useful for LaTeX output or with HTML MathJax options(tinytable_html_mathjax=TRUE).

other

A function to format columns of other types. Defaults to as.character().

replace

Logical, String or Named list of vectors

  • TRUE: Replace NA and NaN by an empty string.

  • FALSE: Print NA and NaN as strings.

  • String: Replace NA and NaN entries by the user-supplied string.

  • Named list: Replace matching elements of the vectors in the list by theirs names. Example:

    • list("-" = c(NA, NaN), "Tiny" = -Inf, "Massive" = Inf)

escape

Logical or "latex" or "html". If TRUE, escape special characters to display them as text in the format of the output of a tt() table.

  • If i and j are both NULL, escape all cells, column names, caption, notes, and spanning labels created by group_tt().

markdown

Logical; if TRUE, render markdown syntax in cells. Ex: _italicized text_ is properly italicized in HTML and LaTeX.

quarto

Logical. Enable Quarto data processing and wrap cell content in a data-qmd span (HTML) or \QuartoMarkdownBase64{} macro (LaTeX). See warnings in the Global Options section below.

fn

Function for custom formatting. Accepts a vector and returns a character vector of the same length.

sprintf

String passed to the ?sprintf function to format numbers or interpolate strings with a user-defined pattern (similar to the glue package, but using Base R).

linebreak

NULL or a single string. If it is a string, replaces that string with appropriate line break sequences depending on the output format (HTML: <br>, LaTeX: \\\\, Typst: \\ ). Markdown output is excluded from line break replacement.

Details

This function formats a vector by passing it to format_tt(). All formatting arguments must be of length 1 or length(x).

Examples

Run this code
# Format numeric vector
format_vector(c(1234.567, 9876.543), digits = 2, num_mark_big = ",")

# Format dates
dates <- as.Date(c("2023-01-01", "2023-12-31"))
format_vector(dates, date = "%B %d, %Y")

# Format logical values
format_vector(c(TRUE, FALSE, TRUE), bool = function(x) ifelse(x, "Yes", "No"))

Run the code above in your browser using DataLab