number_format | R Documentation |
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
).
number_format(ht)
number_format(ht) <- value
set_number_format(ht, row, col, value )
map_number_format(ht, row, col, fn)
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 |
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.
number_format()
returns the number_format
property.
set_number_format()
returns the modified huxtable.
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()
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.