Skip to content

Commit

Permalink
Improved print methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nic-Chr committed Aug 27, 2024
1 parent 9142f86 commit 7ce3ebe
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions R/percent.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,18 @@ signif_half_up <- function(x, digits = 6){

#' @export
as.character.percent <- function(x, digits = 2, ...){
if (length(x) == 0){
character()
} else {
paste0(unclass(round(x, digits) * 100), "%")
}
stringr::str_c(unclass(round(x, digits) * 100), "%")
}

#' @export
format.percent <- function(x, symbol = "%", trim = TRUE,
digits = 2,
...){
if (length(x) == 0){
out <- character()
} else {
out <- paste0(format(unclass(round(x, digits) * 100), trim = trim, digits = NULL, ...),
symbol)
}
out <- stringr::str_c(
format(unclass(round(x, digits) * 100), trim = trim, digits = NULL, ...),
symbol
)
out[is.na(x)] <- NA
names(out) <- names(x)
out
}
Expand All @@ -116,8 +111,11 @@ print.percent <- function(x, max = NULL, trim = TRUE,
max <- min(max, N)
if (max < N) {
out <- out[seq_len(max)]
suffix <- paste(" [ reached 'max' / getOption(\"max.print\") -- omitted",
N - max, "entries ]\n")
suffix <- stringr::str_c(
" [ reached 'max' / getOption(\"max.print\") -- omitted",
N - max, "entries ]\n",
sep = " "
)
}
print(format(out, trim = trim, digits = digits), ...)
cat(suffix)
Expand Down

0 comments on commit 7ce3ebe

Please sign in to comment.