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