huxtable (version 4.5.0)

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)
map_number_format(ht, row, col, fn)

Arguments

ht

A huxtable.

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.

row

A row specifier. See rowspecs for details.

col

An optional column specifier.

fn

A mapping function. See mapping-functions for details.

byrow

Deprecated. Use by_cols() instead.

Value

For number_format, the number_format property. For set_number_format and map_number_format, the modified huxtable.

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
# fixed:
set_number_format(ht_bands, NA)
# alternatively:
huxtable("10000 Maniacs", autoformat = TRUE)


set_number_format(jams, 2)
set_number_format(jams,
      2:3, 1, 2)
map_number_format(jams,
      by_rows(2, 3))
# }

Run the code above in your browser using DataCamp Workspace