Browse Source

Reimplementation of the bytes formatter. Adds flexibility and fixes the problem with NAs in the input.

pull/42/head
Tarcisio 6 years ago
parent
commit
1f8d1a3a09
  1. 81
      R/formatters.r

81
R/formatters.r

@ -1,9 +1,9 @@
#' Bytes formatter: convert to byte measurement and display symbol. #' Bytes formatter: convert to byte measurement and display symbol.
#' #'
#' @return a function with three parameters, \code{x}, a numeric vector that #' @return a function with three parameters, \code{x}, a numeric vector that
#' returns a character vector, \code{symbol} the byte symbol (e.g. "\code{Kb}") #' returns a character vector, \code{symbol} a single or a vector of byte
#' desired and the measurement \code{units} (traditional \code{binary} or #' symbol(s) (e.g. "\code{Kb}") desired and the measurement \code{units}
#' \code{si} for ISI metric units). #' (traditional \code{binary} or \code{si} for ISI metric units).
#' @param x a numeric vector to format #' @param x a numeric vector to format
#' @param symbol byte symbol to use. If "\code{auto}" the symbol used will be #' @param symbol byte symbol to use. If "\code{auto}" the symbol used will be
#' determined by the maximum value of \code{x}. Valid symbols are #' determined by the maximum value of \code{x}. Valid symbols are
@ -12,6 +12,8 @@
#' equivalents and "\code{iB}" equivalents. #' equivalents and "\code{iB}" equivalents.
#' @param units which unit base to use, "\code{binary}" (1024 base) or #' @param units which unit base to use, "\code{binary}" (1024 base) or
#' "\code{si}" (1000 base) for ISI units. #' "\code{si}" (1000 base) for ISI units.
#' @param only_highest Whether to use the unit of the highest number or
#' each number uses its own base.
#' @references Units of Information (Wikipedia) : #' @references Units of Information (Wikipedia) :
#' \url{http://en.wikipedia.org/wiki/Units_of_information} #' \url{http://en.wikipedia.org/wiki/Units_of_information}
#' @export #' @export
@ -21,8 +23,8 @@
#' Kb(sample(3000000000, 10)) #' Kb(sample(3000000000, 10))
#' Mb(sample(3000000000, 10)) #' Mb(sample(3000000000, 10))
#' Gb(sample(3000000000, 10)) #' Gb(sample(3000000000, 10))
byte_format <- function(symbol="auto", units="binary") { byte_format <- function (symbol = "auto", units = "binary", only_highest = TRUE) {
function(x) bytes(x, symbol, units) function(x) bytes(x, symbol, units, only_highest)
} }
#' @export #' @export
@ -39,47 +41,32 @@ Gb <- byte_format("Gb", "binary")
#' @export #' @export
#' @rdname byte_format #' @rdname byte_format
bytes <- function(x, symbol="auto", units=c("binary", "si")) { bytes <- function (x, symbol = 'auto', units = c('binary', 'si'),
only_highest = FALSE) {
symbol <- match.arg(symbol, c("auto", bin_names <- c("bytes", "Kb", "Mb", "Gb", "Tb", "Pb", "Eb", "Zb", "Yb")
"b", "Kb", "Mb", "Gb", "Tb", "Pb", "Eb", "Zb", "Yb", si_names <- c("bytes", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB")
"B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB", symbol <- match.arg(symbol, unique(c("auto", bin_names, toupper(bin_names),
"KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB")) si_names)), several.ok = TRUE)
units <- match.arg(units, c("binary", "si"))
units <- match.arg(units, c("binary", "si")) base <- switch(units, binary = 1024, si = 1000)
out_names <- switch(units, binary = bin_names, si = si_names)
base <- switch(units, `binary`=1024, `si`=1000) sym_len <- length(symbol)
inp_len <- length(x)
if (symbol == "auto") { if (sym_len == 1) {
symbol <- symbol <- rep(symbol, inp_len)
if (max(x) >= (base^5)) { "Pb" } sym_len <- inp_len
else if (max(x) >= (base^4)) { "Tb" }
else if (max(x) >= (base^3)) { "Gb" }
else if (max(x) >= (base^2)) { "Kb" }
else if (max(x) >= (base^1)) { "Mb" }
else { "b" }
} }
if (sym_len != inp_len) {
switch(symbol, stop('Symbols argument must be either long 1 or of the same length as the input vector.')
"b" =, "B" = paste(x, "bytes"), }
symbol <- ifelse(symbol == "auto",
"Kb" =, "KB" = paste(scales::comma(round(x/(base^1), 1L)), "Kb"), pmax(floor(log(x, base)), 0),
"Mb" =, "MB" = paste(scales::comma(round(x/(base^2), 1L)), "Mb"), match(tolower(symbol), tolower(out_names)) - 1)
"Gb" =, "GB" = paste(scales::comma(round(x/(base^3), 1L)), "Gb"), if (only_highest) {
"Tb" =, "TB" = paste(scales::comma(round(x/(base^4), 1L)), "Tb"), symbol <- max(symbol, na.rm = TRUE)
"Pb" =, "PB" = paste(scales::comma(round(x/(base^5), 1L)), "Pb"), }
"Eb" =, "EB" = paste(scales::comma(round(x/(base^6), 1L)), "Eb"), res <- paste(scales::comma(round(x / base^symbol, 1L)),
"Zb" =, "ZB" = paste(scales::comma(round(x/(base^7), 1L)), "Zb"), out_names[symbol + 1])
"Yb" =, "YB" = paste(scales::comma(round(x/(base^8), 1L)), "Yb"), ifelse(!is.na(x), res, x)
"KiB" = paste(scales::comma(round(x/(base^1), 1L)), "KiB"),
"MiB" = paste(scales::comma(round(x/(base^2), 1L)), "MiB"),
"GiB" = paste(scales::comma(round(x/(base^3), 1L)), "GiB"),
"TiB" = paste(scales::comma(round(x/(base^4), 1L)), "TiB"),
"PiB" = paste(scales::comma(round(x/(base^5), 1L)), "PiB"),
"EiB" = paste(scales::comma(round(x/(base^6), 1L)), "EiB"),
"ZiB" = paste(scales::comma(round(x/(base^7), 1L)), "ZiB"),
"YiB" = paste(scales::comma(round(x/(base^8), 1L)), "YiB")
)
} }

Loading…
Cancel
Save