Skip to content

Commit c6e7359

Browse files
committed
Set up basic printing methods
@krlmlr tibble::tibble(x = as.blob(1:5)) uses the default printing method, not the methods I've defined here. What am I doing wrong?
1 parent b21e5ec commit c6e7359

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

NAMESPACE

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ S3method(as.blob,character)
44
S3method(as.blob,integer)
55
S3method(as.blob,list)
66
S3method(as.blob,raw)
7+
S3method(format,blob)
8+
S3method(is_vector_s3,blob)
9+
S3method(obj_sum,blob)
10+
S3method(print,blob)
11+
S3method(type_sum,blob)
712
export(as.blob)
813
export(blob)
914
export(new_blob)
15+
importFrom(tibble,is_vector_s3)
16+
importFrom(tibble,obj_sum)
17+
importFrom(tibble,type_sum)

R/format.R

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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

Comments
 (0)