Skip to contents

Format any character string to HTML or markdown format. We recommend using this format with the ggtext::geom_richtext function to format text in ggplot2 objects.

This function lets the user format numerical values nicely

Usage

formatHTML(text, color = "black", size = 20, bold = FALSE)

formatNum(
  x,
  decimals = 2,
  signif = NULL,
  type = Sys.getenv("LARES_NUMFORMAT"),
  pre = "",
  pos = "",
  sign = FALSE,
  abbr = FALSE,
  ...
)

Arguments

text

Character. Strings to format.

color

Character. Hex colour code.

size

Numeric. Text size.

bold

Boolean. Should the text be bold?

x

Numerical Vector

decimals

Integer. Amount of decimals to display. If set to NULL, then getOption("digits") will be used.

signif

Integer. Rounds the values in its first argument to the specified number of significant digits.

type

Integer. 1 for International standards. 2 for American Standards. Use Sys.setenv("LARES_NUMFORMAT" = 2) to set this parameter globally.

pre, pos

Character. Add string before or after number.

sign

Boolean. Add + sign to positive values.

abbr

Boolean. Abbreviate using num_abbr()? You can use the `decimals` parameter to set abbr's n(-1) parameter.

...

Additional lazy eval parameters.

Value

String with format characters included.

Character. String vector with reformatted continuous numbers

Examples

formatHTML("Text test", color = "#000000")
#> [1] "<span style='font-size:20px; color:#000000'>Text test</span>"
formatHTML(c(123, 456), color = "orange", size = 120, bold = TRUE)
#> [1] "<span style='font-size:120px; color:orange'>**123**<br/>**456**</span>"

# If you want to use it with \code{ggtext}:
if (FALSE) {
col1 <- "grey"
col2 <- "orange"
pt <- data.frame(
  label = paste0(
    formatHTML(123, color = col2, size = 120, bold = TRUE), "<br/>",
    formatHTML("of children had a", col1), "<br/>",
    formatHTML("traditional stay-at-home mom", color = col2, bold = TRUE), "<br/>",
    formatHTML(paste0("in 2012, compared to ", 321, " in 1970"), color = col1)
  )
)
ggplot(pt, aes(x = 0, y = 0)) +
  ggtext::geom_richtext(
    aes(label = label),
    hjust = 0,
    label.color = NA,
    lineheight = 1.5
  ) +
  xlim(0, 0.01) +
  theme_void()
}
formatNum(1.23456, decimals = 3)
#> [1] "1.235"
formatNum(1.23456, type = 1)
#> [1] "1,23"
formatNum(1.23456, pre = "$", pos = "/person")
#> [1] "$1.23/person"
formatNum(123456, abbr = TRUE)
#> [1] "123K"
formatNum(1234567890, abbr = TRUE, signif = 2)
#> [1] "1.2B"
formatNum(1234567890, decimals = 0, abbr = TRUE)
#> [1] "1B"
formatNum(c(-3:3), sign = TRUE)
#> [1] "-3" "-2" "-1" "0"  "+1" "+2" "+3"