Debian Codegraph @

R/scientific.R

raw
1 format_exp <- function(exp, si) {
2 if (si) {
3 si(exp)
4 } else {
5 supernum(exp)
6 }
7 }
8
9 si <- function(x) {
10 num <- !is.na(x)
11 if (!any(num)) {
12 return(rep_along(x, ""))
13 }
14
15 if (all(x[num] == 0L)) {
16 return(rep_along(x, ""))
17 }
18
19 si_prefixes <- c(
20 "y", "z", "a", "f", "p", "n", "\u00b5", "m", " ", "k", "M", "G", "T", "P", "E", "Z", "Y"
21 )
22
23 idx <- (x / 3) + 9
24 idx[is.na(idx)] <- 9
25 style_bold(si_prefixes[idx])
26 }
27
28 supernum <- function(x) {
29 stopifnot(is.integer(x))
30
31 num <- !is.na(x)
32 if (!any(num)) {
33 return(rep_along(x, ""))
34 }
35
36 neg <- num & x < 0
37 if (any(neg)) {
38 neg_chr <- ifelse(neg, "-", "+")
39 neg_chr[!num] <- " "
40 } else {
41 neg_chr <- rep("", length(x))
42 }
43
44 digits <- as.character(abs(x))
45 digits[!num] <- ""
46
47 exp <- paste0(neg_chr, format(digits, justify = "right"))
48
49 paste0(style_subtle(ifelse(num, "e", " ")), style_num(exp, neg))
50 }