number_format: Set how numbers are formatted in cells

number_formatR Documentation

Set how numbers are formatted in cells

Description

If number_format is:

  • numeric, numbers will be rounded to that many decimal places;

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

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

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

Usage

number_format(ht)
number_format(ht) <- value
set_number_format(ht, row, col, value )
map_number_format(ht, row, col, fn)

Arguments

ht

A huxtable.

row

A row specifier. See rowspecs for details.

col

An optional column specifier.

fn

A mapping function. See mapping-functions for details.

value

A character or integer vector,

Note that setting to NA does not reset to the default.

Details

Number formatting is applied to any parts of cells that look like numbers. The exception is exponents in scientific notation; huxtable attempts to detect and ignore these.

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.

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.

To set number_format to a function, enclose the function in list. The function should take one argument and return a string. fmt_pretty() and fmt_percent() are useful shortcuts for common formatting functions.

Value

number_format() returns the number_format property. set_number_format() returns the modified huxtable.

See Also

fmt_pretty() and fmt_percent().options("huxtable.long_minus") in huxtable-options for pretty-printing minus signs.

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

Examples

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)
      )

number_format(ht)[3, -1] <- NA
number_format(ht)[4, -1] <- 2
number_format(ht)[5, -1] <- "%5.2f"

number_format(ht)[6, -1] <- fmt_pretty()

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
# fixed:
set_number_format(ht_bands, NA)


huxtable documentation built on Oct. 2, 2024, 1:06 a.m.