Debian Codegraph @

R/ctl_new_pillar_list.R

raw
1 #' Customize the appearance of compound pillars in your tibble subclass
2 #'
3 #' @description
4 #' `r lifecycle::badge("experimental")`
5 #'
6 #' Gain full control over the appearance of the pillars of your tibble subclass
7 #' in its body.
8 #' This method is intended for implementers of subclasses of the `"tbl"` class.
9 #' Users will rarely need them, and we also expect the default implementation
10 #' to be sufficient for the vast majority of cases.
11 #'
12 #' @details
13 #' `ctl_new_pillar_list()` is called to construct a list of pillars.
14 #' If `x` is a regular (one-dimensional) vector, the list contains one pillar
15 #' constructed by [ctl_new_pillar()].
16 #' This method also works for compound columns: columns that are data frames,
17 #' matrices or arrays, with the following behavior:
18 #'
19 #' - If `width` is `NULL`, the method always returns a list of length one
20 #' containing one pillar object that represents the first sub-column in this
21 #' compound column.
22 #' - Otherwise, the returned list contains one pillar object for all sub-columns
23 #' that can be fit in the available horizontal space.
24 #' These pillar objects are obtained by calling `ctl_new_pillar_list()`
25 #' with `width = NULL` on each sub-column until the available width is
26 #' exhausted.
27 #'
28 #' This method is called to initiate the construction of all pillars
29 #' in the tibble to be printed.
30 #' To ensure that all packed columns that fit the available space are printed,
31 #' `ctl_new_pillar_list()` may be called twice on the same input:
32 #' once with `width = NULL`, and
33 #' once with `width` corresponding to the then known available space
34 #' and with `first_pillar` set to the pillar object constructed in the
35 #' first call.
36 #'
37 #' @inheritParams ctl_new_pillar
38 #' @param x A vector, can also be a data frame, matrix, or array.
39 #' @param width The available width, can be a vector for multiple tiers.
40 #' If `NULL`, only the first pillar is instantiated.
41 #' @param first_pillar Can be passed to this method if the first pillar
42 #' for a compound pillar (or the pillar itself for a simple pillar)
43 #' has been constructed already.
44 #' @export
45 #' @examplesIf rlang::is_installed(c("palmerpenguins", "tibble")) && requireNamespace("tibble")
46 #' # Simple column
47 #' ctl_new_pillar_list(
48 #' tibble::tibble(),
49 #' palmerpenguins::penguins$weight[1:3],
50 #' width = 10
51 #' )
52 #'
53 #' # Packed data frame: unknown width
54 #' ctl_new_pillar_list(
55 #' tibble::tibble(),
56 #' palmerpenguins::penguins[1:3, ],
57 #' width = NULL
58 #' )
59 #'
60 #' # Packed data frame: known width
61 #' ctl_new_pillar_list(
62 #' tibble::tibble(),
63 #' palmerpenguins::penguins,
64 #' width = 60
65 #' )
66 #'
67 #' # Deeply packed data frame with known width:
68 #' # showing only the first sub-column even if the width is sufficient
69 #' ctl_new_pillar_list(
70 #' tibble::tibble(),
71 #' tibble::tibble(x = tibble::tibble(b = 1, c = 2), y = 3),
72 #' width = 60
73 #' )
74 #'
75 #' # Packed matrix: unknown width
76 #' ctl_new_pillar_list(tibble::tibble(), matrix(1:6, ncol = 2), width = NULL)
77 #'
78 #' # Packed matrix: known width
79 #' ctl_new_pillar_list(tibble::tibble(), matrix(1:6, ncol = 2), width = 60)
80 #'
81 #' # Packed array
82 #' ctl_new_pillar_list(tibble::tibble(), Titanic, width = 60)
83 ctl_new_pillar_list <- function(controller, x, width, ..., title = NULL, first_pillar = NULL) {
84 "!!!!DEBUG ctl_new_pillar_list(`v(width)`, `v(title)`)"
85
86 check_dots_empty()
87
88 UseMethod("ctl_new_pillar_list")
89 }
90
91 #' @export
92 ctl_new_pillar_list.tbl <- function(controller, x, width, ..., title = NULL, first_pillar = NULL) {
93 "!!!!DEBUG ctl_new_pillar_list.tbl(`v(width)`, `v(title)`)"
94
95 if (is.data.frame(x)) {
96 new_data_frame_pillar_list(x, controller, width, title = title, first_pillar = first_pillar)
97 } else if (is.matrix(x) && !inherits(x, c("Surv", "Surv2"))) {
98 new_matrix_pillar_list(x, controller, width, title = title, first_pillar = first_pillar)
99 } else if (is.array(x) && length(dim(x)) > 2) {
100 new_array_pillar_list(x, controller, width, title = title, first_pillar = first_pillar)
101 } else {
102 if (is.null(first_pillar)) {
103 first_pillar <- ctl_new_pillar(controller, x, width, ..., title = prepare_title(title))
104 }
105 new_single_pillar_list(first_pillar, width)
106 }
107 }
108
109 new_data_frame_pillar_list <- function(x, controller, width, title, first_pillar = NULL) {
110 "!!!!!DEBUG new_data_frame_pillar_list(`v(width)`, `v(title)`)"
111
112 if (ncol(x) == 0) {
113 pillar <- pillar_from_shaft(
114 new_pillar_title(prepare_title(title)),
115 new_pillar_type(x),
116 new_empty_shaft(nrow(x)),
117 width
118 )
119 return(new_single_pillar_list(pillar, width))
120 }
121
122 max_n_pillars <- sum(width %/% 2)
123 pillars <- vector("list", max_n_pillars)
124
125 ticked_names <- tick_if_needed(names(x))
126
127 for (i in seq_along(x)) {
128 "!!!!!DEBUG i = `i`, width = `width`"
129
130 if (i == 1 && !is.null(first_pillar)) {
131 pillar <- first_pillar
132 } else {
133 # Call ctl_new_pillar_list(), return only the first sub-pillar
134 # thanks to width = NULL
135 new_pillars <- ctl_new_pillar_list(
136 controller, x[[i]],
137 width = NULL,
138 title = c(title, ticked_names[[i]])
139 )
140
141 # Safety check:
142 if (length(new_pillars) == 0) {
143 # NULL return: doesn't fit
144 break
145 }
146
147 pillar <- new_pillars[[1]]
148 }
149
150 if (is.null(pillar)) {
151 # NULL return: doesn't fit
152 break
153 }
154
155 if (is.null(width)) {
156 return(list(pillar))
157 }
158
159 # Compute remaining width
160 new_width <- deduct_width(width, pillar_get_min_width(pillar))
161 if (is.null(new_width)) {
162 # NULL return: current pillar doesn't fit
163 break
164 }
165
166 pillars[[i]] <- pillar
167 width <- new_width
168
169 if (!is.null(title)) {
170 title[] <- ""
171 }
172 }
173
174 pillars <- compact(pillars)
175
176 new_pillar_list(
177 pillars,
178 extra = names(x)[seq2(length(pillars) + 1, length(x))],
179 remaining_width = width
180 )
181 }
182
183 new_matrix_pillar_list <- function(x, controller, width, title, first_pillar = NULL) {
184 if (ncol(x) == 0) {
185 pillar <- pillar_from_shaft(
186 new_pillar_title(prepare_title(title)),
187 new_pillar_type(x),
188 new_empty_shaft(nrow(x)),
189 width
190 )
191 return(new_single_pillar_list(pillar, width))
192 }
193
194 max_n_pillars <- sum(width %/% 2)
195 pillars <- vector("list", max_n_pillars)
196
197 idx <- colnames(x)
198 if (is.null(idx)) {
199 idx <- seq_len(ncol(x))
200 } else {
201 idx <- encodeString(idx, quote = '"')
202 }
203
204 ticked_names <- paste0("[,", idx, "]")
205
206 for (i in seq_along(ticked_names)) {
207 if (i == 1 && !is.null(first_pillar)) {
208 pillar <- first_pillar
209 } else {
210 # FIXME
211 # sub_title <- c(title, ticked_names[[i]])
212 sub_title <- ticked_names[[i]]
213 if (!is.null(title)) {
214 if (i > 1) {
215 title[[length(title)]] <- ""
216 }
217 sub_title <- c(title, sub_title)
218 }
219
220 # Call ctl_new_pillar() only for objects that can fit
221 pillar <- ctl_new_pillar(
222 controller, x[, i],
223 width = NULL,
224 title = prepare_title(sub_title)
225 )
226 }
227
228 if (is.null(pillar)) {
229 # NULL return: doesn't fit
230 break
231 }
232
233 if (is.null(width)) {
234 return(list(pillar))
235 }
236
237 # Compute remaining width
238 new_width <- deduct_width(width, pillar_get_min_width(pillar))
239 if (is.null(new_width)) {
240 # NULL return: current pillar doesn't fit
241 break
242 }
243
244 pillars[[i]] <- pillar
245 width <- new_width
246 }
247
248 pillars <- compact(pillars)
249 new_pillar_list(
250 pillars,
251 extra = seq2(length(pillars) + 1, ncol(x)),
252 remaining_width = width
253 )
254 }
255
256 new_array_pillar_list <- function(x, controller, width, title, first_pillar = NULL) {
257 if (!is.null(first_pillar)) {
258 return(list(first_pillar))
259 }
260
261 first_slice <- head(as.vector(x), vec_size(x))
262
263 body <- pillar_shaft(first_slice)
264
265 pillar <- pillar_from_shaft(
266 new_pillar_title(title),
267 new_pillar_type(x),
268 new_continuation_shaft(body),
269 width
270 )
271 new_single_pillar_list(pillar, width)
272 }
273
274 # Can be rewritten with a repeat loop
275 deduct_width <- function(width, consumed_widths) {
276 "!!!!!DEBUG deduct_width(`v(width)`, `v(consumed_widths)`)"
277
278 if (length(consumed_widths) == 0) {
279 # All sub-pillars distributed
280 width
281 } else if (length(width) == 0) {
282 # Doesn't fit
283 NULL
284 } else if (width[[1]] >= consumed_widths[[1]]) {
285 # Fits first tier
286 deduct_width(
287 c(max(width[[1]] - consumed_widths[[1]] - 1L, 0), width[-1]),
288 consumed_widths[-1]
289 )
290 } else {
291 # Doesn't fit first tier: try next tier
292 deduct_width(width[-1], consumed_widths)
293 }
294 }
295
296 new_single_pillar_list <- function(pillar, width) {
297 pillar_width <- pillar_get_min_width(pillar)
298 if (is.null(width)) {
299 new_width <- NULL
300 } else {
301 new_width <- deduct_width(width, pillar_width)
302 stopifnot(!is.null(new_width))
303 }
304 new_pillar_list(
305 list(pillar),
306 extra = NULL,
307 remaining_width = new_width,
308 simple = TRUE
309 )
310 }
311
312 new_pillar_list <- function(pillar_list, extra, remaining_width, simple = FALSE) {
313 structure(
314 pillar_list,
315 extra = extra,
316 remaining_width = remaining_width,
317 simple = simple
318 )
319 }
320
321 prepare_title <- function(title) {
322 n_title <- length(title)
323 if (n_title == 0) {
324 title
325 } else if (grepl("^[[]", title[[n_title]])) {
326 paste0(paste(title[-n_title], collapse = "$"), title[[n_title]])
327 } else {
328 paste(title, collapse = "$")
329 }
330 }