Debian Codegraph @

R/type-sum.R

raw
1 #' Provide a succinct summary of an object
2 #'
3 #' `type_sum()` gives a brief summary of object type. Objects that commonly
4 #' occur in a data frame should return a string with four or less characters.
5 #' For most inputs, the argument is forwarded to [vctrs::vec_ptype_abbr()].
6 #'
7 #' When formatting a pillar,
8 #' `type_sum()` will be called on a slice of the column vector.
9 #' The formatted type should only depend on the type and not on the data,
10 #' to avoid confusion.
11 #'
12 #' @param x an object to summarise. Generally only methods of atomic vectors
13 #' and variants have been implemented.
14 #'
15 #' @export
16 type_sum <- function(x) {
17 UseMethod("type_sum")
18 }
19
20 #' @export
21 type_sum.ordered <- function(x) {
22 # r-lib/vctrs#323:
23 type_sum.default(x)
24 }
25
26 #' @export
27 type_sum.factor <- function(x) {
28 # r-lib/vctrs#323:
29 "fct"
30 }
31
32 #' @export
33 type_sum.default <- function(x) {
34 pillar_attr <- attr(x, "pillar", exact = TRUE)
35
36 label <- pillar_attr$label
37 if (!is.null(label)) {
38 return(I(label))
39 }
40
41 if (is.object(x) || vec_is(x)) {
42 return(vec_ptype_abbr(x))
43 }
44
45 switch(typeof(x),
46 builtin = ,
47 special = ,
48 closure = "fn",
49 environment = "env",
50 symbol =
51 if (is_missing(x)) {
52 "missing"
53 } else {
54 "sym"
55 },
56 typeof(x)
57 )
58 }
59
60 #' @export
61 vec_ptype_abbr.pillar_empty_col <- function(x, ...) {
62 vec_ptype_abbr(x[[1]])
63 }
64
65 #' @description
66 #' `obj_sum()` also includes the size (but not the shape) of the object
67 #' if [vctrs::vec_is()] is `TRUE`.
68 #' It should always return a string (a character vector of length one).
69 #' As of pillar v1.6.1, the default method forwards to [vctrs::vec_ptype_abbr()]
70 #' for vectors and to [type_sum()] for other objects.
71 #' Previous versions always forwarded to [type_sum()].
72 #' An attribute named `"short"` in the return value will be picked up by
73 #' the [pillar_shaft()] method for lists, and used if space is limited.
74 #'
75 #' @examples
76 #' obj_sum(1:10)
77 #' obj_sum(matrix(1:10))
78 #' obj_sum(data.frame(a = 1))
79 #' obj_sum(Sys.Date())
80 #' obj_sum(Sys.time())
81 #' obj_sum(mean)
82 #' @rdname type_sum
83 #' @export
84 obj_sum <- function(x) {
85 UseMethod("obj_sum")
86 }
87
88 #' @export
89 obj_sum.default <- function(x) {
90 if (!vec_is(x)) {
91 type_sum(x)
92 } else {
93 abbr <- vec_ptype_abbr(x, suffix_shape = FALSE)
94
95 out <- paste(abbr, size_sum(x))
96 if (is.array(x)) {
97 short <- paste0(abbr, "[", symbol$ellipsis, "]")
98 } else {
99 short <- abbr
100 }
101
102 structure(out, short = short)
103 }
104 }
105
106 #' @export
107 obj_sum.AsIs <- function(x) {
108 paste0("I(", obj_sum(remove_as_is_class(x)), ")")
109 }
110
111 #' @description
112 #' `size_sum()` is called by `obj_sum()` to format the size of the object.
113 #' It should always return a string (a character vector of length one),
114 #' it can be an empty string `""` to omit size information,
115 #' this is what the default method does for scalars.
116 #'
117 #' @keywords internal
118 #' @examples
119 #'
120 #' size_sum(1:10)
121 #' size_sum(trees)
122 #' size_sum(Titanic)
123 #' @rdname type_sum
124 #' @export
125 size_sum <- function(x) {
126 UseMethod("size_sum")
127 }
128
129 #' @export
130 size_sum.default <- function(x) {
131 if (!vec_is(x)) {
132 return("")
133 }
134
135 paste0("[", dim_desc(x), "]")
136 }