Learn R Programming

huxtable (version 4.2.1)

number_format: Number format

Description

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

Usage

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

Arguments

ht

A huxtable.

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

A character or integer vector, or a list containing a function, or NA. Note that setting to NA does not reset to the default.

Value

For number_format, the number_format property. 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;

  • character, it will be taken as an argument to sprintf();

  • a function, the function will be applied to the numbers;

  • NA, then numbers will not be formatted (except maybe by conversion with as.character).

Note that if your cells are of type numeric, a number format of NA doesn't guarantee you get back what you typed in, since R's default conversion may apply scientific notation and rounding.

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

To set number_format to a function, enclose the function in list. The function should take one argument and return a string.

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(
  number_format = c("Default", "NA", "2", "\"%5.2f\"", "Pretty", "Sign"),
  a = rep(1000, 6),
  b = rep(1000.005, 6),
  c = rep(0.0001, 6),
  d = rep(-1, 6),
  e = rep("3.2 (s.e. 1.4)", 6),
  add_colnames = TRUE
)
number_format(ht)[3, -1] <- NA
number_format(ht)[4, -1] <- 2
number_format(ht)[5, -1] <- '%5.2f'
number_format(ht)[6, -1] <- list(function(x) prettyNum(x, big.mark = ',', scientific = FALSE))
number_format(ht)[7, -1] <- list(function(x) if(x>0) '+' else '-')
right_border(ht) <- 1
bottom_border(ht)[1, ] <- 1
ht

ht_bands <- huxtable("10000 Maniacs", autoformat = FALSE)
# probably not what you want:
ht_bands
number_format(ht_bands) <- NA
ht_bands
# alternatively:
huxtable("10000 Maniacs", autoformat = TRUE)

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