|
| 1 | +#' @export |
| 2 | +format.blob <- function(x, ...) { |
| 3 | + if (length(x) == 0) |
| 4 | + return(character()) |
| 5 | + |
| 6 | + paste0("blob[", blob_size(x, ...) , "]") |
| 7 | +} |
| 8 | + |
| 9 | +#' @export |
| 10 | +print.blob <- function(x, ...) { |
| 11 | + if (length(x) == 0) { |
| 12 | + cat("blob()\n") |
| 13 | + } else { |
| 14 | + print(format(x, ...), quote = FALSE) |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +#' @export |
| 19 | +#' @importFrom tibble type_sum |
| 20 | +type_sum.blob <- function(x) { |
| 21 | + "blob" |
| 22 | +} |
| 23 | + |
| 24 | +#' @export |
| 25 | +#' @importFrom tibble obj_sum |
| 26 | +obj_sum.blob <- function(x) { |
| 27 | + format(x, trim = FALSE) |
| 28 | +} |
| 29 | + |
| 30 | +#' @export |
| 31 | +#' @importFrom tibble is_vector_s3 |
| 32 | +is_vector_s3.blob <- function(x) TRUE |
| 33 | + |
| 34 | +blob_size <- function(x, digits = 3, trim = TRUE) { |
| 35 | + x <- vapply(x, length, integer(1)) |
| 36 | + |
| 37 | + power <- min(floor(log(abs(x), 1000)), 4) |
| 38 | + if (power < 1) { |
| 39 | + unit <- "B" |
| 40 | + } else { |
| 41 | + unit <- c("kb", "Mb", "Gb", "Tb")[[power]] |
| 42 | + x <- x / (1024 ^ power) |
| 43 | + } |
| 44 | + |
| 45 | + x1 <- signif(x, digits = digits) |
| 46 | + x2 <- format(x1, big.mark = ",", scientific = FALSE, trim = trim) |
| 47 | + paste0(x2, " ", unit) |
| 48 | +} |
0 commit comments