| 1 |
# Format numbers in decimal notation |
| 2 |
# |
| 3 |
# This formatting system is designed to make it as easy as possible to |
| 4 |
# compare columns of numbers. Significant digits are coloured black or red |
| 5 |
# (for positive and negative numbers) and non-significant digits are coloured |
| 6 |
# in paler gray. |
| 7 |
# |
| 8 |
# @return A list with at least the following elements: |
| 9 |
# * `neg`: negative sign or space, `TRUE` if needed |
| 10 |
# * `lhs`: whole number |
| 11 |
# * `dec`: decimal point, `TRUE` if needed |
| 12 |
# * `rhs`: remainder of number |
| 13 |
# |
| 14 |
# @param x A numeric vector |
| 15 |
# @param sigfig Number of significant figures to display. |
| 16 |
# @param digits Number of digits after the decimal point, incompatible with |
| 17 |
# `sigfig`. A negative number means the maximum number of digits to display. |
| 18 |
# @param sci_mod Set to 1 for scientific or to 3 for engineering display. |
| 19 |
# @param si Set to `TRUE` for SI notation, requires `sci_mod = 3` |
| 20 |
# @param fixed Set to `TRUE` to use the same exponent everywhere. |
| 21 |
# @seealso [split_decimal()] |
| 22 |
# @examples |
| 23 |
# split_decimal(1.5:3.5) |
| 24 |
# split_decimal(1.5:3.5, sci_mod = 1) |
| 25 |
# split_decimal(1e9) |
| 26 |
# split_decimal(1e9, sci_mod = 1) |
| 27 |
split_decimal <- function(x, sigfig, digits = NULL, sci_mod = NULL, si = FALSE, |
| 28 |
fixed_exponent = NULL) { |
| 29 |
"!!!!!!DEBUG split_decimal(`v(x)`, `v(sigfig)`, `v(digits)`, `v(sci_mod)`, `v(si)`, `v(fixed_exponent)`" |
| 30 |
|
| 31 |
stopifnot(is.numeric(x)) |
| 32 |
sigfig <- check_sigfig(sigfig) |
| 33 |
|
| 34 |
num <- is.finite(x) |
| 35 |
"!!!!!!DEBUG `v(num)`" |
| 36 |
dec <- num |
| 37 |
|
| 38 |
# Do we need negative signs? |
| 39 |
neg <- !is.na(x) & x < 0 |
| 40 |
"!!!!!!DEBUG `v(neg)`" |
| 41 |
|
| 42 |
abs_x <- abs(x) |
| 43 |
mnt <- abs_x |
| 44 |
"!!!!!!DEBUG `v(mnt)`" |
| 45 |
|
| 46 |
if (!is.null(sci_mod)) { |
| 47 |
exp <- compute_exp_display(mnt, sigfig, digits) |
| 48 |
exp <- fix_exp(num, exp, fixed_exponent, sci_mod, si) |
| 49 |
"!!!!!!DEBUG `v(exp)`" |
| 50 |
unit <- attr(exp, "unit") |
| 51 |
|
| 52 |
# Must divide by 10^exp, because 10^-exp may not be representable |
| 53 |
# for very large values of exp |
| 54 |
mnt_idx <- which(num & mnt != 0) |
| 55 |
mnt[mnt_idx] <- safe_divide_10_to(mnt[mnt_idx], exp[mnt_idx]) |
| 56 |
"!!!!!!DEBUG `v(mnt)`" |
| 57 |
} else { |
| 58 |
exp <- 0 |
| 59 |
"!!!!!!DEBUG `v(exp)`" |
| 60 |
unit <- NULL |
| 61 |
} |
| 62 |
|
| 63 |
if (is.null(sci_mod) || !is.null(fixed_exponent)) { |
| 64 |
exp_display <- rep_along(x, NA_integer_) |
| 65 |
} else { |
| 66 |
exp_display <- exp |
| 67 |
} |
| 68 |
|
| 69 |
if (is.null(digits)) { |
| 70 |
"!!!!!!DEBUG `v(sigfig)`" |
| 71 |
min_sigfig <- compute_min_sigfig(mnt) |
| 72 |
"!!!!!!DEBUG `v(min_sigfig)`" |
| 73 |
round_mnt <- safe_signif(mnt, pmax(sigfig, min_sigfig, na.rm = TRUE)) |
| 74 |
"!!!!!!DEBUG `v(round_mnt)`" |
| 75 |
rhs_digits <- compute_rhs_digits(mnt, sigfig) |
| 76 |
} else if (digits >= 0) { |
| 77 |
"!!!!!!DEBUG `v(digits)`" |
| 78 |
round_mnt <- round(mnt, digits) |
| 79 |
rhs_digits <- digits |
| 80 |
} else { |
| 81 |
"!!!!!!DEBUG `v(-digits)`" |
| 82 |
round_mnt <- round(mnt, -digits) |
| 83 |
trunc_mnt <- trunc(mnt) |
| 84 |
rhs_digits <- compute_rhs_digits(mnt - trunc_mnt, -digits, trunc_mnt) |
| 85 |
} |
| 86 |
|
| 87 |
"!!!!!!DEBUG `v(round_mnt)`" |
| 88 |
"!!!!!!DEBUG `v(rhs_digits)`" |
| 89 |
|
| 90 |
lhs <- trunc(round_mnt) |
| 91 |
"!!!!!!DEBUG `v(lhs)`" |
| 92 |
|
| 93 |
rhs <- round_mnt - lhs |
| 94 |
"!!!!!!DEBUG `v(rhs)`" |
| 95 |
|
| 96 |
if (is.null(digits) || digits < 0) { |
| 97 |
"!!!!!!DEBUG `v(lhs * 10^exp - abs_x)`" |
| 98 |
reset_dec <- (mnt == 0 | (rhs == 0 & within_tolerance(lhs * 10^exp, abs_x))) |
| 99 |
"!!!!!!DEBUG `v(reset_dec)`" |
| 100 |
|
| 101 |
dec[reset_dec] <- FALSE |
| 102 |
"!!!!!!DEBUG `v(dec)`" |
| 103 |
} |
| 104 |
|
| 105 |
ret <- list( |
| 106 |
sigfig = sigfig, |
| 107 |
num = num, |
| 108 |
neg = neg, |
| 109 |
# integer64 doesn't support format(trim = FALSE) |
| 110 |
# trimws() is unnecessarily slow |
| 111 |
lhs = sub("^ +", "", format(lhs, scientific = FALSE)), |
| 112 |
lhs_zero = (lhs == 0), |
| 113 |
rhs = rhs, |
| 114 |
rhs_digits = rhs_digits, |
| 115 |
dec = dec, |
| 116 |
exp = exp_display, |
| 117 |
unit = unit, |
| 118 |
si = si |
| 119 |
) |
| 120 |
|
| 121 |
set_width(ret, get_decimal_width(ret)) |
| 122 |
} |
| 123 |
|
| 124 |
fix_exp <- function(num, exp, fixed_exponent, sci_mod, si) { |
| 125 |
"!!!!!!DEBUG fix_exp(`v(num)`, `v(exp)`, `v(fixed_exponent)`, `v(sci_mod)`, `v(si)`" |
| 126 |
if (!is.null(fixed_exponent)) { |
| 127 |
if (is.finite(fixed_exponent)) { |
| 128 |
exp <- fixed_exponent |
| 129 |
} else if (all(is.na(exp))) { |
| 130 |
exp <- NA_real_ |
| 131 |
} else if (fixed_exponent < 0) { |
| 132 |
exp <- min(exp, na.rm = TRUE) |
| 133 |
} else { |
| 134 |
exp <- max(exp, na.rm = TRUE) |
| 135 |
} |
| 136 |
"!!!!!!DEBUG `v(exp)`" |
| 137 |
exp <- as.integer(round(exp)) |
| 138 |
exp <- structure(rep_along(num, exp), unit = exp %|% 0L) |
| 139 |
exp[!num] <- NA_integer_ |
| 140 |
"!!!!!!DEBUG `v(exp)`" |
| 141 |
} |
| 142 |
|
| 143 |
if (sci_mod != 1) { |
| 144 |
exp[] <- as.integer(round(floor(exp / sci_mod) * sci_mod)) |
| 145 |
"!!!!!!DEBUG `v(exp)`" |
| 146 |
} |
| 147 |
if (si) { |
| 148 |
# Truncate very small and very large exponents |
| 149 |
exp[] <- pmin(pmax(exp, -24L), 24L) |
| 150 |
"!!!!!!DEBUG `v(exp)`" |
| 151 |
} |
| 152 |
|
| 153 |
exp |
| 154 |
} |
| 155 |
|
| 156 |
get_decimal_width <- function(x) { |
| 157 |
exp <- x$exp[!is.na(x$exp)] |
| 158 |
|
| 159 |
if (x$si) { |
| 160 |
exp_digits <- any(exp != 0) |
| 161 |
} else { |
| 162 |
exp_digits <- any(exp < 0) + max(2 + trunc(log10(abs(exp) + 0.5)), 0) |
| 163 |
} |
| 164 |
|
| 165 |
max(x$neg + nchar(x$lhs), 0) + |
| 166 |
any(x$dec, na.rm = TRUE) + |
| 167 |
max(x$rhs_digits, 0) + |
| 168 |
exp_digits |
| 169 |
} |
| 170 |
|
| 171 |
safe_signif <- function(x, digits) { |
| 172 |
if (length(x) == 0L) { |
| 173 |
return(numeric()) |
| 174 |
} |
| 175 |
signif(x, digits) |
| 176 |
} |
| 177 |
|
| 178 |
safe_divide_10_to <- function(x, y) { |
| 179 |
# Computes x / 10^y in a robust way |
| 180 |
|
| 181 |
10^(log10(x) - y) |
| 182 |
} |
| 183 |
|
| 184 |
eps_2 <- 2 * .Machine$double.eps |
| 185 |
|
| 186 |
within_tolerance <- function(x, y) { |
| 187 |
"!!!!!!DEBUG within_tolerance(`v(x)`, `v(y)`)" |
| 188 |
l2x <- round(log2(x)) |
| 189 |
"!!!!!!DEBUG `v(l2x)`" |
| 190 |
l2y <- round(log2(y)) |
| 191 |
"!!!!!!DEBUG `v(l2y)`" |
| 192 |
|
| 193 |
equal <- (l2x == l2y) |
| 194 |
equal[is.na(equal)] <- FALSE |
| 195 |
out <- equal |
| 196 |
|
| 197 |
# Work around integer64 problem |
| 198 |
equal[x == y] <- FALSE |
| 199 |
"!!!!!!DEBUG `v(abs((x[equal] - y[equal]) * 2 ^ -l2x[equal]))`" |
| 200 |
out[equal] <- abs((x[equal] - y[equal]) * 2^-l2x[equal]) <= eps_2 |
| 201 |
out |
| 202 |
} |
| 203 |
|
| 204 |
compute_rhs_digits <- function(x, sigfig, offset = rep_along(x, 0)) { |
| 205 |
"!!!!!!DEBUG compute_rhs_digits(`v(x)`, `v(sigfig)`)" |
| 206 |
# If already bigger than sigfig, can round to zero. |
| 207 |
# Otherwise ensure we have sigfig digits shown |
| 208 |
exp <- compute_exp(x, sigfig) |
| 209 |
exp[is.na(exp)] <- Inf |
| 210 |
"!!!!!!DEBUG `v(exp)" |
| 211 |
rhs_digits <- rep_along(x, 0) |
| 212 |
"!!!!!!DEBUG `v(rhs_digits)" |
| 213 |
|
| 214 |
if (!is.integer(x) && !all(x == trunc(x), na.rm = TRUE)) { |
| 215 |
has_rhs <- (exp <= sigfig) |
| 216 |
"!!!!!!DEBUG `v(has_rhs)`" |
| 217 |
rhs_digits[has_rhs] <- sigfig - 1 - exp[has_rhs] |
| 218 |
|
| 219 |
to_check <- rhs_digits > 0 |
| 220 |
while (any(to_check, na.rm = TRUE)) { |
| 221 |
"!!!!!!DEBUG `v(to_check)" |
| 222 |
"!!!!!!DEBUG `v(rhs_digits)" |
| 223 |
|
| 224 |
which_to_check <- which(to_check) |
| 225 |
val <- (x[which_to_check] + offset[which_to_check]) * 10^(rhs_digits[which_to_check] - 1) |
| 226 |
"!!!!!!DEBUG `v(val)" |
| 227 |
"!!!!!!DEBUG `v(val - round(val))" |
| 228 |
|
| 229 |
resid_zero <- within_tolerance(val, round(val)) |
| 230 |
"!!!!!!DEBUG `v(resid_zero)`" |
| 231 |
resid_zero[is.na(resid_zero)] <- FALSE |
| 232 |
|
| 233 |
rhs_digits[which_to_check][resid_zero] <- |
| 234 |
rhs_digits[which_to_check][resid_zero] - 1 |
| 235 |
|
| 236 |
to_check[which_to_check][!resid_zero] <- FALSE |
| 237 |
to_check[rhs_digits == 0] <- FALSE |
| 238 |
} |
| 239 |
|
| 240 |
"!!!!!!DEBUG `v(to_check)" |
| 241 |
"!!!!!!DEBUG `v(rhs_digits)" |
| 242 |
} |
| 243 |
|
| 244 |
"!!!!!!DEBUG `v(rhs_digits)" |
| 245 |
rhs_digits |
| 246 |
} |
| 247 |
|
| 248 |
compute_min_sigfig <- function(x) { |
| 249 |
ret <- rep_along(x, NA_integer_) |
| 250 |
nonzero <- which(x != 0 & is.finite(x)) |
| 251 |
ret[nonzero] <- as.integer(floor(log10(x[nonzero]))) + 1L |
| 252 |
ret |
| 253 |
} |
| 254 |
|
| 255 |
compute_extra_sigfig <- function(x) { |
| 256 |
x <- sort(abs(x)) |
| 257 |
delta <- diff(x) |
| 258 |
x <- x[-1] |
| 259 |
|
| 260 |
keep <- which((delta != 0) & is.finite(delta)) |
| 261 |
if (length(keep) == 0) { |
| 262 |
return(0) |
| 263 |
} |
| 264 |
|
| 265 |
ceiling(log10(max(x[keep] / delta[keep]))) - 1 |
| 266 |
} |
| 267 |
|
| 268 |
LOG_10 <- log(10) |
| 269 |
|
| 270 |
compute_exp_display <- function(x, sigfig, digits = NULL) { |
| 271 |
"!!!!!!DEBUG compute_exp_display(`v(x)`, `v(sigfig)`, `v(digits)`)" |
| 272 |
if (is.null(sigfig)) { |
| 273 |
sigfig <- abs(digits) |
| 274 |
} |
| 275 |
|
| 276 |
# With 3 significant digits: |
| 277 |
# 0.9994 -> 0.999 -> exp == -1 |
| 278 |
# 0.9995 -> 1.00 -> exp == 0 |
| 279 |
# This means that x is divided by 0.9995 in this example |
| 280 |
# before computing log10(). |
| 281 |
# Division before log is the same as subtraction after log. |
| 282 |
# Using log1p for numerical stability. |
| 283 |
offset <- log1p(-5 * 10^(-sigfig - 1)) / LOG_10 |
| 284 |
"!!!!!!DEBUG `v(offset)`" |
| 285 |
|
| 286 |
ret <- rep_along(x, NA_integer_) |
| 287 |
nonzero <- which(x != 0 & is.finite(x)) |
| 288 |
ret[nonzero] <- as.integer(floor(log10(x[nonzero]) - offset)) |
| 289 |
"!!!!!!DEBUG `v(ret)`" |
| 290 |
ret |
| 291 |
} |
| 292 |
|
| 293 |
compute_exp <- function(x, sigfig) { |
| 294 |
"!!!!!!DEBUG compute_exp(`v(x)`, `v(sigfig)`)" |
| 295 |
ret <- rep_along(x, NA_integer_) |
| 296 |
nonzero <- which(x != 0 & is.finite(x)) |
| 297 |
ret[nonzero] <- as.integer(floor(log10(x[nonzero]))) |
| 298 |
"!!!!!!DEBUG `v(ret)`" |
| 299 |
ret |
| 300 |
} |
| 301 |
|
| 302 |
format_mantissa <- function(x) { |
| 303 |
lhs <- format_lhs(x) |
| 304 |
dec <- format_dec(x) |
| 305 |
rhs <- format_rhs(x) |
| 306 |
paste0(lhs, dec, rhs) |
| 307 |
} |
| 308 |
|
| 309 |
format_neg <- function(s) { |
| 310 |
neg <- s$neg |
| 311 |
neg_col <- ifelse(neg, "-", "") |
| 312 |
neg_col |
| 313 |
} |
| 314 |
|
| 315 |
format_lhs <- function(s) { |
| 316 |
neg <- s$neg |
| 317 |
num <- s$num |
| 318 |
lhs_zero <- s$lhs_zero |
| 319 |
|
| 320 |
lhs_str <- s$lhs |
| 321 |
lhs_split <- strsplit(lhs_str, "", fixed = TRUE) |
| 322 |
lhs_width <- lengths(lhs_split) |
| 323 |
|
| 324 |
lhs_split_underlined <- map(lhs_split, underline_3_back) |
| 325 |
|
| 326 |
lhs_sig <- map(pmin(lhs_width, s$sigfig), seq_len) |
| 327 |
lhs_insig <- map(lhs_sig, `-`) |
| 328 |
|
| 329 |
lhs_split_sig <- map2(lhs_split_underlined, lhs_sig, `[`) |
| 330 |
lhs_split_non <- map2(lhs_split_underlined, lhs_insig, `[`) |
| 331 |
|
| 332 |
lhs_sig <- map_chr(lhs_split_sig, paste, collapse = "") |
| 333 |
lhs_non <- map_chr(lhs_split_non, paste, collapse = "") |
| 334 |
|
| 335 |
# as.character() to support corner case of length zero |
| 336 |
lhs_col <- as.character(ifelse(num, |
| 337 |
paste0( |
| 338 |
style_num(lhs_sig, neg, !lhs_zero), |
| 339 |
style_subtle_num(lhs_non, neg) |
| 340 |
), |
| 341 |
style_na(lhs_str) |
| 342 |
)) |
| 343 |
|
| 344 |
lhs_col <- paste0(format_neg(s), lhs_col) |
| 345 |
# No alignment here, result needs to be right-aligned |
| 346 |
lhs_col |
| 347 |
} |
| 348 |
|
| 349 |
underline_3_back <- function(x) { |
| 350 |
idx <- which(trunc((seq_along(x) - length(x)) / 3) %% 2 == 1) |
| 351 |
x[idx] <- crayon_underline(x[idx]) |
| 352 |
x |
| 353 |
} |
| 354 |
|
| 355 |
format_dec <- function(s) { |
| 356 |
neg <- s$neg |
| 357 |
dec <- s$dec |
| 358 |
lhs_zero <- s$lhs_zero |
| 359 |
|
| 360 |
# Decimal column |
| 361 |
if (any(dec)) { |
| 362 |
dec_col <- ifelse(dec, style_num(".", neg, !lhs_zero), " ") |
| 363 |
} else { |
| 364 |
dec_col <- rep_along(neg, "") |
| 365 |
} |
| 366 |
dec_col |
| 367 |
} |
| 368 |
|
| 369 |
format_rhs <- function(s) { |
| 370 |
neg <- s$neg |
| 371 |
dec <- s$dec |
| 372 |
lhs_zero <- s$lhs_zero |
| 373 |
rhs_digits <- s$rhs_digits |
| 374 |
|
| 375 |
# Digits on RHS of . |
| 376 |
|
| 377 |
# s$rhs * 10^(rhs_digits) should be almost an integer |
| 378 |
rhs_val <- s$rhs * 10^rhs_digits |
| 379 |
|
| 380 |
rhs_num <- sprintf("%.0f", abs(round(rhs_val))) |
| 381 |
rhs_num[rhs_num == "0"] <- "" |
| 382 |
|
| 383 |
# Special treatment for leading zeros for some formatting, |
| 384 |
# hand-rolling |
| 385 |
n_zeros <- pmax(0, rhs_digits - get_extent(rhs_num)) |
| 386 |
rhs_zero <- strrep("0", n_zeros) |
| 387 |
|
| 388 |
rhs_split <- strsplit(paste0(rhs_zero, rhs_num), "", fixed = TRUE) |
| 389 |
rhs_split_underlined <- map(rhs_split, underline_3) |
| 390 |
|
| 391 |
rhs_is_zero <- map(n_zeros, seq_len) |
| 392 |
rhs_is_nonzero <- map2(n_zeros + 1L, rhs_digits, seq2) |
| 393 |
|
| 394 |
rhs_split_underlined_zero <- map2(rhs_split_underlined, rhs_is_zero, `[`) |
| 395 |
rhs_split_underlined_num <- map2(rhs_split_underlined, rhs_is_nonzero, `[`) |
| 396 |
|
| 397 |
rhs_underlined_zero <- map_chr(rhs_split_underlined_zero, paste, collapse = "") |
| 398 |
rhs_underlined_num <- map_chr(rhs_split_underlined_num, paste, collapse = "") |
| 399 |
|
| 400 |
rhs_col <- ifelse(dec, |
| 401 |
paste0( |
| 402 |
style_num(rhs_underlined_zero, neg, significant = !lhs_zero), |
| 403 |
style_num(rhs_underlined_num, neg) |
| 404 |
), |
| 405 |
"" |
| 406 |
) |
| 407 |
|
| 408 |
# ensure all same width |
| 409 |
rhs_col <- align(rhs_col, max(rhs_digits, 0L, na.rm = TRUE), "left") |
| 410 |
|
| 411 |
rhs_col |
| 412 |
} |
| 413 |
|
| 414 |
underline_3 <- function(x) { |
| 415 |
idx <- which(trunc((seq_along(x) - 1) / 3) %% 2 == 1) |
| 416 |
x[idx] <- crayon_underline(x[idx]) |
| 417 |
x |
| 418 |
} |
| 419 |
|
| 420 |
#' @export |
| 421 |
#' @param negative,significant Logical vector the same length as `x` that |
| 422 |
#' indicate if the values are negative and significant, respectively |
| 423 |
#' @rdname style_subtle |
| 424 |
#' @examples |
| 425 |
#' style_num( |
| 426 |
#' c("123", "456"), |
| 427 |
#' negative = c(TRUE, FALSE) |
| 428 |
#' ) |
| 429 |
#' style_num( |
| 430 |
#' c("123", "456"), |
| 431 |
#' negative = c(TRUE, FALSE), |
| 432 |
#' significant = c(FALSE, FALSE) |
| 433 |
#' ) |
| 434 |
style_num <- function(x, negative, significant = rep_along(x, TRUE)) { |
| 435 |
ifelse(significant, ifelse(negative, style_neg(x), x), style_subtle_num(x, negative)) |
| 436 |
} |
| 437 |
|
| 438 |
assemble_decimal <- function(x) { |
| 439 |
mantissa <- format_mantissa(x) |
| 440 |
exp <- format_exp(x$exp, x$si) |
| 441 |
|
| 442 |
paste0(mantissa, exp) |
| 443 |
} |
| 444 |
|
| 445 |
#' @export |
| 446 |
format.pillar_shaft_decimal <- function(x, width, ...) { |
| 447 |
if (is.null(x$dec) || width < get_width(x$dec)) { |
| 448 |
fmt <- x$sci |
| 449 |
} else { |
| 450 |
fmt <- x$dec |
| 451 |
} |
| 452 |
|
| 453 |
if (length(fmt$num) == 0L) { |
| 454 |
return(character()) |
| 455 |
} |
| 456 |
|
| 457 |
if (width < get_min_width(fmt)) { |
| 458 |
stop( |
| 459 |
"Need at least width ", get_min_width(x), ", requested ", width, ".", |
| 460 |
call. = FALSE |
| 461 |
) |
| 462 |
} |
| 463 |
|
| 464 |
row <- assemble_decimal(fmt) |
| 465 |
|
| 466 |
used_width <- get_max_extent(row) |
| 467 |
row <- paste0(strrep(" ", width - used_width), row) |
| 468 |
new_ornament(row, width = width, align = "right") |
| 469 |
} |