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
)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
digitssignificant 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 MathJaxoptions(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
NAandNaNby an empty string.FALSE: Print
NAandNaNas strings.String: Replace
NAandNaNentries 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
iandjare bothNULL, escape all cells, column names, caption, notes, and spanning labels created bygroup_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-qmdspan (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
?sprintffunction to format numbers or interpolate strings with a user-defined pattern (similar to thegluepackage, 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
# Format numeric vector
format_vector(c(1234.567, 9876.543), digits = 2, num_mark_big = ",")
#> [1] "1,235" "9,877"
# Format dates
dates <- as.Date(c("2023-01-01", "2023-12-31"))
format_vector(dates, date = "%B %d, %Y")
#> [1] "January 01, 2023" "December 31, 2023"
# Format logical values
format_vector(c(TRUE, FALSE, TRUE), bool = function(x) ifelse(x, "Yes", "No"))
#> [1] "Yes" "No" "Yes"