From eab07ff0a6f7ff380a56378a000e785be6399fa3 Mon Sep 17 00:00:00 2001 From: Vincent Arel-Bundock Date: Wed, 17 Jan 2024 13:47:21 -0500 Subject: [PATCH] suffix --- R/format_tt.R | 29 ++++++++++++++++++++++++++++- inst/tinytest/test-format_tt.R | 6 ++---- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/R/format_tt.R b/R/format_tt.R index 7a6250ce..5955aff0 100644 --- a/R/format_tt.R +++ b/R/format_tt.R @@ -39,6 +39,7 @@ format_tt <- function(x = NULL, digits = NULL, num_fmt = "significant", num_zero = TRUE, + num_suffix = FALSE, num_mark_big = "", num_mark_dec = getOption("OutDec", default = "."), url = FALSE, @@ -99,15 +100,23 @@ format_tt <- function(x = NULL, # numeric } else if (is.numeric(x[[col]]) && !is.null(digits)) { - if (num_fmt == "significant") { + + if (isTRUE(num_suffix)) { + x[[col]] <- format_num_suffix( + x[[col]], digits = digits, num_mark_big = num_mark_big, num_mark_dec = num_mark_dec, num_zero = num_zero + ) + + } else if (num_fmt == "significant") { x[[col]] <- format(x[[col]], digits = digits, drop0trailing = !num_zero, big.mark = num_mark_big, decimal.mark = num_mark_dec, scientific = FALSE) + } else if (num_fmt == "decimal") { x[[col]] <- formatC(x[[col]], digits = digits, format = "f", drop0trailing = !num_zero, big.mark = num_mark_big, decimal.mark = num_mark_dec) + } else if (num_fmt == "scientific") { x[[col]] <- formatC(x[[col]], digits = digits, format = "e", drop0trailing = !num_zero, @@ -127,3 +136,21 @@ format_tt <- function(x = NULL, } } + + + +format_num_suffix <- function(x, digits, num_mark_big, num_mark_dec, num_zero) { + suffix <- number <- rep("", length(x)) + suffix <- ifelse(x > 1e12, "T", suffix) + suffix <- ifelse(x > 1e9, "B", suffix) + suffix <- ifelse(x > 1e6, "M", suffix) + suffix <- ifelse(x > 1e3, "K", suffix) + number <- format_tt(x, num_fmt = "decimal", digits = digits, num_mark_big = num_mark_big, num_mark_dec = num_mark_dec, num_zero = num_zero) + number <- ifelse(x > 1e12, format_tt(x / 1e12, num_fmt = "decimal", digits = digits, num_mark_big = num_mark_big, num_mark_dec = num_mark_dec, num_zero = num_zero), number) + number <- ifelse(x > 1e9, format_tt(x / 1e9, num_fmt = "decimal", digits = digits, num_mark_big = num_mark_big, num_mark_dec = num_mark_dec, num_zero = num_zero), number) + number <- ifelse(x > 1e6, format_tt(x / 1e6, num_fmt = "decimal", digits = digits, num_mark_big = num_mark_big, num_mark_dec = num_mark_dec, num_zero = num_zero), number) + number <- ifelse(x > 1e3, format_tt(x / 1e3, num_fmt = "decimal", digits = digits, num_mark_big = num_mark_big, num_mark_dec = num_mark_dec, num_zero = num_zero), number) + number <- ifelse(x > 1e3, format_tt(x / 1e3, num_fmt = "decimal", digits = digits, num_mark_big = num_mark_big, num_mark_dec = num_mark_dec, num_zero = num_zero), number) + number <- paste0(number, suffix) + return(number) +} diff --git a/inst/tinytest/test-format_tt.R b/inst/tinytest/test-format_tt.R index b07a493d..8ea589c5 100644 --- a/inst/tinytest/test-format_tt.R +++ b/inst/tinytest/test-format_tt.R @@ -6,9 +6,7 @@ dat <- data.frame( z = sample(c(TRUE, FALSE), N, replace = TRUE) ) -# pkgload::load_all() +pkgload::load_all() dat |> - format_tt(digits = 4) |> + format_tt(digits = 2, num_suffix = TRUE) |> tt() - -format_tt(dat$x, digits = 1)