Skip to contents

Similar to the style_*() family of functions, but these return a style_*() function rather than performing the styling. Use them wherever an argument expects a formatting function: tbl_summary(digits =), add_p(pvalue_fun =), modify_fmt_fun() and inline_text(pvalue_fun =).

Usage

label_style_number(
  digits = 0,
  big.mark = ifelse(decimal.mark == ",", " ", ","),
  decimal.mark = getOption("OutDec"),
  scale = 1,
  prefix = "",
  suffix = "",
  na = NA_character_,
  ...
)

label_style_percent(
  prefix = "",
  suffix = "",
  digits = 0,
  big.mark = ifelse(decimal.mark == ",", " ", ","),
  decimal.mark = getOption("OutDec"),
  na = NA_character_,
  ...
)

label_style_pvalue(
  digits = 1,
  prepend_p = FALSE,
  big.mark = ifelse(decimal.mark == ",", " ", ","),
  decimal.mark = getOption("OutDec"),
  na = NA_character_,
  ...
)

label_style_sigfig(
  digits = 2,
  scale = 1,
  big.mark = ifelse(decimal.mark == ",", " ", ","),
  decimal.mark = getOption("OutDec"),
  prefix = "",
  suffix = "",
  na = NA_character_,
  ...
)

label_style_ratio(
  digits = 2,
  big.mark = ifelse(decimal.mark == ",", " ", ","),
  decimal.mark = getOption("OutDec"),
  prefix = "",
  suffix = "",
  na = NA_character_,
  ...
)

Arguments

digits

(integer)
Number of decimal places. Recycled to the length of x.

big.mark

(string)
Character used between every three digits to the left of the decimal mark.

decimal.mark

(string)
Character used as the decimal mark.

scale

(scalar numeric)
Values are multiplied by scale before rounding, e.g. scale = 100 to show a proportion as a percentage.

prefix, suffix

(string)
Text added before and after each formatted value.

na

(string)
Replacement for missing values.

...

Not used. Accepted so that formatting functions can be called interchangeably.

prepend_p

(scalar logical)
Whether to prefix the value with "p=" (or "p" when the value starts with < or >).

Value

A function that takes a numeric vector and returns a character vector.

Examples

my_style <- label_style_number(digits = 1)
my_style(3.14)
#> [1] "3.1"

# Example 1 ----------------------------------
# formatting functions as `digits` and `pvalue_fun`
trial |>
  tbl_summary(
    by = trt,
    include = c(age, marker),
    statistic = all_continuous() ~ "{mean} ({sd})",
    digits = list(age = label_style_number(digits = 1), marker = label_style_sigfig(digits = 3)),
    missing = "no"
  ) |>
  add_p(test = everything() ~ "t.test", pvalue_fun = label_style_pvalue(digits = 3))