Learn R Programming

huxtable (version 4.0.1)

number_format: Number format

Description

Functions to get or set the number format property of huxtable table cells.

Usage

number_format(ht)
number_format(ht) <- value
set_number_format(ht, row, col, value, byrow = FALSE)

Arguments

ht

A huxtable.

value

A vector or list which may be character, numeric or function. See below. Set to NA to reset to the default.

row

A row specifier. See rowspecs for details.

col

An optional column specifier.

byrow

If TRUE, fill in values by row rather than by column.

Value

For number_format, the number_format attribute. For set_number_format, the ht object.

Details

Number formatting is applied to any parts of cells that look like numbers (defined as an optional minus sign, followed by numerals, followed by an optional decimal point and further numerals). The exception is exponents in scientific notation; huxtable attempts to detect and ignore these.

If value is numeric, numbers will be rounded to that many decimal places. If value is character, it will be taken as an argument to sprintf(). If value is a function it will be applied to the numbers and should return a string. If value is NA, then numbers will be unchanged. Note that setting value to NA does not reset to the default.

The default value is "%.3g" which rounds numbers if they have more than 3 significant digits, and which may use an exponent for large numbers.

To set number_format to a function, enclose the function in list. See the examples.

Versions of huxtable before 2.0.0 applied number_format only to cells that looked like numbers in their entirety. The default value was "%5.2f".

See Also

Other formatting functions: background_color, bold, font_size, font, na_string, text_color

Examples

Run this code
# NOT RUN {
ht <- huxtable(a = 10^(3:6) + (5 * 10^(-2:-5)), b = 10^(3:6) + (5* 10^(-2:-5)))
number_format(ht)[1,] <- 2
number_format(ht)[2,] <- '%5.2f'
number_format(ht)[3,] <- list(function(x) prettyNum(x, big.mark = ','))
number_format(ht)[4,] <- list(function(x) if(x>0) '+' else '-')
ht
print_screen(ht)
ht_bands <- huxtable("10000 Maniacs")
ht_bands # probably not what you want
number_format(ht_bands) <- NA
ht_bands

ht <- huxtable(a = 1:3, b = 3:1)
set_number_format(ht, 2)
set_number_format(ht, 1:2, 1, 2)
set_number_format(ht, 1:2, 1:2, c(2, 3), byrow = TRUE)
set_number_format(ht, where(ht == 1), 2)
# }

Run the code above in your browser using DataLab