knitr::opts_chunk$set( collapse = TRUE, comment = "#>", warning = FALSE, message = FALSE )
Rbearcat provides six bcat_fmt_* functions for formatting numbers in inline
R expressions and tables. They wrap functions from the scales package with
sensible defaults.
| Function | Formats as |
|---|---|
| bcat_fmt_dollar() | Currency ($1,234.56) |
| bcat_fmt_percent() | Percentage (12.3%) |
| bcat_fmt_comma() | Comma-separated (1,234) |
| bcat_fmt_scientific() | Scientific notation (1.23e+06) |
| bcat_fmt_date() | Human-readable date (March 10, 2026) |
| bcat_fmt_pvalue() | P-value with </> notation |
library(Rbearcat)
bcat_fmt_dollar()bcat_fmt_dollar(c(1234.5, 67890, 0.99))
The default uses a hyphen. Set style_negative = "parens" for accounting style:
bcat_fmt_dollar(c(-500, 1200)) bcat_fmt_dollar(c(-500, 1200), style_negative = "parens")
Use scale to convert units (e.g., raw cents to dollars):
bcat_fmt_dollar(c(150000, 275000), scale = 1e-3, accuracy = 1, suffix = "K")
bcat_fmt_percent()Multiplies by 100 by default (assumes proportions as input):
bcat_fmt_percent(c(0.0523, 0.1, 0.9871))
If values are already in percentage form, set scale = 1:
bcat_fmt_percent(c(5.23, 10, 98.71), scale = 1)
bcat_fmt_percent(c(0.05234, 0.10011), accuracy = 0.01)
bcat_fmt_comma()bcat_fmt_comma(c(1000, 50000, 1234567))
bcat_fmt_comma(c(5000, 10000, 80000), scale = 1e-3, accuracy = 1, suffix = "K")
bcat_fmt_scientific()bcat_fmt_scientific(c(0.00012, 3456789, 1.5e10))
bcat_fmt_scientific(c(123456, 789012), digits = 2)
bcat_fmt_date()Converts character or Date objects to a human-readable format. The default
format is "%B %e, %Y" (e.g., "January 5, 2026").
bcat_fmt_date(Sys.Date()) bcat_fmt_date(c("2024-01-15", "2025-06-30"))
Use standard strptime codes:
bcat_fmt_date("2025-12-25", format = "%d %b %Y") bcat_fmt_date("2025-12-25", format = "%m/%d/%Y")
bcat_fmt_pvalue()Uses < and > notation for extreme values:
bcat_fmt_pvalue(c(0.54, 0.045, 0.001, 0.00001))
Useful in inline reporting:
bcat_fmt_pvalue(c(0.032, 0.0001), add_p = TRUE)
bcat_fmt_pvalue(c(0.0456, 0.00012), accuracy = 0.01)
These formatters are most powerful inside inline R expressions in RMarkdown or Quarto. For example, suppose you compute some values:
avg_price <- 12345.67 pct_change <- 0.052 my_pval <- 0.003 bcat_fmt_dollar(avg_price) bcat_fmt_percent(pct_change) bcat_fmt_pvalue(my_pval, add_p = TRUE)
In your RMarkdown document you would reference these with inline R code like
`r "\u0060r bcat_fmt_dollar(avg_price)\u0060"` to produce formatted
numbers directly in your prose:
The average price was
r bcat_fmt_dollar(avg_price)with ar bcat_fmt_percent(pct_change)year-over-year change. The coefficient was significant (r bcat_fmt_pvalue(my_pval, add_p = TRUE)).
The bcat_fmt_* functions can also serve as label functions for ggplot2 axis
scales:
library(ggplot2) set_UC_geoms() ggplot(economics, aes(date, pce)) + geom_line() + scale_y_continuous(labels = bcat_fmt_dollar) + labs(title = "Personal Consumption Expenditure", x = NULL, y = "Billions ($)") + theme_UC()
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.