huxtable (version 1.2.0)

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

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 cell contents. Number format is applied to any cells that look like numbers (as judged by as.numeric), not just to numeric cells. This allows you to do e.g. ht <- huxtable(c('Salary', 35000, 32000, 40000)) and still format numbers correctly. To set number_format to a function, enclose the function in list. See the examples.

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 <- huxtable(a = 1:3, b = 3:1)
ht2 <- set_number_format(ht, 2)
number_format(ht2)
ht3 <- set_number_format(ht, 1:2, 1, 2)
number_format(ht3)
ht4 <- set_number_format(ht, 1:2, 1:2, c(2, 3), byrow = TRUE)
number_format(ht4)
ht5 <- set_number_format(ht, where(ht == 1), 2)
number_format(ht5)
# }

Run the code above in your browser using DataCamp Workspace