Skip to content

Commit

Permalink
suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentarelbundock committed Jan 17, 2024
1 parent 58c570b commit eab07ff
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
29 changes: 28 additions & 1 deletion R/format_tt.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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)
}
6 changes: 2 additions & 4 deletions inst/tinytest/test-format_tt.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit eab07ff

Please sign in to comment.