Debian Codegraph @

R/ctl_new_pillar.R

raw
1 #' Customize the appearance of simple 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.
10 #'
11 #' @details
12 #' `ctl_new_pillar()` is called to construct pillars for regular (one-dimensional)
13 #' vectors.
14 #' The default implementation returns an object constructed with [pillar()].
15 #' Extend this method to modify the pillar components returned from the default
16 #' implementation.
17 #' Override this method to completely change the appearance of the pillars.
18 #' Components are created with [new_pillar_component()] or [pillar_component()].
19 #' In order to customize printing of row IDs, a method can be supplied for the
20 #' `ctl_new_rowid_pillar()` generic.
21 #'
22 #' All components must be of the same height.
23 #' This restriction may be levied in the future.
24 #'
25 #' Implementations should return `NULL` if none of the data
26 #' fits the available width.
27 #'
28 #' @inheritParams rlang::args_dots_empty
29 #' @param controller The object of class `"tbl"` currently printed.
30 #' @param x A simple (one-dimensional) vector.
31 #' @param width The available width, can be a vector for multiple tiers.
32 #' @param title The title, derived from the name of the column in the data.
33 #'
34 #' @seealso
35 #' See [ctl_new_pillar_list()] for creating pillar objects for compound columns:
36 #' packed data frames, matrices, or arrays.
37 #'
38 #' @export
39 #' @examplesIf rlang::is_installed(c("palmerpenguins", "tibble"))
40 #' # Create pillar objects
41 #' ctl_new_pillar(
42 #' palmerpenguins::penguins,
43 #' palmerpenguins::penguins$species[1:3],
44 #' width = 60
45 #' )
46 #'
47 #' ctl_new_pillar(
48 #' palmerpenguins::penguins,
49 #' palmerpenguins::penguins$bill_length_mm[1:3],
50 #' width = 60
51 #' )
52 #' @examples
53 #'
54 #' # Customize output
55 #' lines <- function(char = "-") {
56 #' stopifnot(nchar(char) == 1)
57 #' structure(char, class = "lines")
58 #' }
59 #'
60 #' format.lines <- function(x, width, ...) {
61 #' paste(rep(x, width), collapse = "")
62 #' }
63 #'
64 #' ctl_new_pillar.line_tbl <- function(controller, x, width, ...) {
65 #' out <- NextMethod()
66 #' new_pillar(list(
67 #' title = out$title,
68 #' type = out$type,
69 #' lines = new_pillar_component(list(lines("=")), width = 1),
70 #' data = out$data
71 #' ))
72 #' }
73 #'
74 #' ctl_new_rowid_pillar.line_tbl <- function(controller, x, width, ...) {
75 #' out <- NextMethod()
76 #' new_pillar(
77 #' list(
78 #' title = out$title,
79 #' type = out$type,
80 #' lines = new_pillar_component(list(lines("=")), width = 1),
81 #' data = out$data
82 #' ),
83 #' width = as.integer(floor(log10(max(nrow(x), 1))) + 1)
84 #' )
85 #' }
86 #'
87 #' vctrs::new_data_frame(
88 #' list(a = 1:3, b = letters[1:3]),
89 #' class = c("line_tbl", "tbl")
90 #' )
91 #'
92 #' ctl_new_rowid_pillar.roman_tbl <- function(controller, x, width, ...) {
93 #' out <- NextMethod()
94 #' rowid <- utils::as.roman(seq_len(nrow(x)))
95 #' width <- max(nchar(as.character(rowid)))
96 #' new_pillar(
97 #' list(
98 #' title = out$title,
99 #' type = out$type,
100 #' data = pillar_component(
101 #' new_pillar_shaft(list(row_ids = rowid),
102 #' width = width,
103 #' class = "pillar_rif_shaft"
104 #' )
105 #' )
106 #' ),
107 #' width = width
108 #' )
109 #' }
110 #'
111 #' vctrs::new_data_frame(
112 #' list(a = 1:3, b = letters[1:3]),
113 #' class = c("roman_tbl", "tbl")
114 #' )
115 #'
116 ctl_new_pillar <- function(controller, x, width, ..., title = NULL) {
117 "!!!!DEBUG ctl_new_pillar(`v(width)`, `v(title)`)"
118
119 check_dots_empty()
120
121 UseMethod("ctl_new_pillar")
122 }
123
124 #' @export
125 ctl_new_pillar.tbl <- function(controller, x, width, ..., title = NULL) {
126 "!!!!DEBUG ctl_new_pillar.tbl(`v(width)`, `v(title)`)"
127
128 pillar(x, title, if (!is.null(width)) max0(width))
129 }
130
131 #' @param type String for specifying a row ID type. Current values in use are
132 #' `NULL` and `"*"`.
133 #' @rdname ctl_new_pillar
134 #' @export
135 ctl_new_rowid_pillar <- function(controller, x, width, ..., title = NULL, type = NULL) {
136 "!!!!DEBUG ctl_new_rowid_pillar(`v(width)`, `v(title)`)"
137
138 check_dots_empty()
139
140 if (length(width) == 0) {
141 return(NULL)
142 }
143
144 UseMethod("ctl_new_rowid_pillar")
145 }
146
147 #' @export
148 ctl_new_rowid_pillar.tbl <- function(controller, x, width, ..., title = NULL, type = NULL) {
149 "!!!!DEBUG ctl_new_rowid_pillar.tbl(`v(width)`, `v(title)`)"
150
151 template <- names(ctl_new_pillar(controller, vector(), width, title = title))
152
153 if (!length(template)) {
154 return(NULL)
155 }
156
157 data <- rif_shaft(nrow(x))
158
159 out <- map(set_names(template), function(.x) "")
160
161 if ("type" %in% template) {
162 out$type <- pillar_component(rif_type(identical(type, "*")))
163 }
164
165 if ("data" %in% template) {
166 out$data <- pillar_component(data)
167 }
168
169 new_pillar(out, width = get_width(data))
170 }
171
172 max0 <- function(x) {
173 if (length(x) > 0) {
174 max(x)
175 } else {
176 0L
177 }
178 }