dbosak01 / fmtr

An R package for formatting data.
12 stars 1 forks source link

Support for SI unit formatting #55

Closed mavam closed 1 year ago

mavam commented 1 year ago

I'm looking to format numbers with an SI suffix, similar to label_bytes from the scales package.

Is this supported? I couldn't find it in the docs.

dbosak01 commented 1 year ago

You can put a suffix in the format. Like this:

v1 <- c(10, 5, 36)

fapply(v1, "%.1f MB")

[1] "10.0 MB" "5.0 MB" "36.0 MB"

But I don't have an "auto_si" like scales does. That is an interesting idea though.

mavam commented 1 year ago

Okay, good to know. I think in this case I have a working solution, I was just curious if fmtr can do it natively.

fmt_si <- function(x) scales::label_number_si(accuracy = 0.1)(x)

Yields:

> fmt_si(4.2^(1:10))
 [1] "4.2"    "17.6"   "74.1"   "311.2"  "1.3K"   "5.5K"   "23.1K"  "96.8K"  "406.7K" "1.7M"