Debian Codegraph @

vignettes/printing.Rmd

raw
1 ---
2 title: "Printing a tibble: Control and data flow"
3 output: html_vignette
4 vignette: >
5 %\VignetteIndexEntry{Printing a tibble: Control and data flow}
6 %\VignetteEngine{knitr::rmarkdown}
7 %\VignetteEncoding{UTF-8}
8 ---
9
10 ```{r, include = FALSE}
11 knitr::opts_chunk$set(
12 collapse = TRUE,
13 eval = rlang::is_installed("tibble"),
14 comment = "#>"
15 )
16
17 pillar:::set_show_source_hooks()
18 ```
19
20 ```{r setup}
21 library(pillar)
22 ```
23
24 What happens when a tibble is printed?
25 This vignette documents the control flow and the data flow, explains the design choices, and shows the default implementation for the `"tbl"` class.
26 It is mainly of interest for implementers of table subclasses.
27 Customizing the formatting of a vector class in a tibble is described in `vignette("pillar", package = "vctrs")`.
28 The different customization options are showcased in `vignette("extending")`.
29
30
31 ## Requirements
32
33 - Fit into pre-specified width, distributing across multiple tiers if necessary
34
35 - Optionally shrink and stretch individual columns
36
37 - Header, body and footer for the tibble
38
39 - Avoid recomputation of information
40
41 - Custom components for the pillars in a tibble, top-aligned
42
43 - The container, not the column vectors, determine the appearance
44
45 - Customization of the entire output and of the pillars
46
47 - Support for data frame columns (packed data frames) and matrix/array columns
48
49 - Pillars are always shown from left to right, no "holes" in the colonnade
50
51 - If the first column consumes all available space, the remaining columns are not shown, even if they all would fit if the first column is omitted.
52
53 - Printing pillars should take time proportional to the number of characters printed, and be "fast enough".
54
55
56
57 ## Overview
58
59 The overall control and data flow are illustrated in the diagram below.
60 Boxes are functions and methods.
61 Solid lines are function calls.
62 Dotted lines represent information that a function obtains via argument or (in the case of options) queries actively.
63
64 ```{r echo = FALSE, error = TRUE}
65 text <- paste(
66 readLines("format.mmd"),
67 collapse = "\n"
68 )
69 DiagrammeR::mermaid(text)
70 ```
71
72 The pillar package uses debugme for debugging.
73 Activating debugging for pillar is another way to track the control flow, see `vignette("debugme")` for details.
74
75
76 ## Initialization
77
78 A tibble is a list of columns of class `"tbl_df"` and `"tbl"`.
79 Printing is designed to work for non-data-frame table-like objects such as lazy tables.
80 The `print.tbl()` method calls `format()` for the object and prints the output.
81
82 ```{r}
83 tbl <- tibble::tibble(a = 1:3, b = tibble::tibble(c = 4:6, d = 7:9), e = 10:12)
84 print(tbl, width = 23)
85 str(tbl)
86 ```
87
88
89 ```{r show_source = TRUE}
90 pillar:::print.tbl
91 ```
92
93 The `format.tbl()` method creates a setup object, and uses that object to format header, body and footer.
94
95 ```{r show_source = TRUE}
96 pillar:::format.tbl
97 ```
98
99 While it's possible to extend or override these methods for your `"tbl"` subclass, often overriding the more specialized methods shown below is sufficient.
100
101
102 ## Setup
103
104 Most of the work for formatting actually happens in `tbl_format_setup()`.
105 The desired output width is baked into the setup object and must be available when calling.
106 Setup objects print like a tibble but with a clear separation of header, body, and footer.
107
108 ```{r}
109 setup <- tbl_format_setup(tbl, width = 24)
110 setup
111 ```
112
113 A setup object is required here to avoid computing information twice.
114 For instance, the dimensions shown in the header or the extra columns displayed in the footer are available only after the body has been computed.
115
116 The generic dispatches over the container, so that you can override it if necessary.
117 It is responsible for assigning default values to arguments before passing them on to the method.
118
119 ```{r show_source = TRUE}
120 tbl_format_setup
121 ```
122
123
124 The default implementation converts the input to a data frame via `as.data.frame(head(x))`, and returns an object constructed with `new_tbl_format_setup()` that contains the data frame and additional information.
125 If you override this method, e.g. to incorporate more information, you can add new items to the default setup object, but you should not overwrite existing items.
126
127 ```{r show_source = TRUE}
128 pillar:::tbl_format_setup.tbl
129 ```
130
131 At the core, the internal function `ctl_colonnade()` composes the body.
132 Its functionality and the customization points it offers are detailed in the "Colonnade" section below.
133
134
135 ## Header, body, footer
136
137 The components of a tibble are formatted with `tbl_format_*()` generics, which also dispatch on the container to allow extension or overrides.
138 They return a character vector, with one element per line printed.
139 The setup object is required.
140
141 ```{r}
142 tbl_format_header(tbl, setup)
143 tbl_format_body(tbl, setup)
144 tbl_format_footer(tbl, setup)
145 ```
146
147 (The body is returned as a classed object with a `print()` method, it is still a `character()` under the hood.)
148
149 ```{r}
150 class(tbl_format_body(tbl, setup))
151 typeof(tbl_format_body(tbl, setup))
152 ```
153
154 Since most of the work already has been carried out in `tbl_format_setup()`, the default implementations mostly consist of code that styles and wraps the output.
155
156 ```{r show_source = TRUE}
157 pillar:::tbl_format_header.tbl
158 ```
159
160 ```{r show_source = TRUE}
161 pillar:::tbl_format_body.tbl
162 ```
163
164 ```{r show_source = TRUE}
165 pillar:::tbl_format_footer.tbl
166 ```
167
168
169 ## Colonnade
170
171 The internal function `ctl_colonnade()` composes the body.
172 It performs the following tasks:
173
174 1. Create a pillar object for every top-level column that fits, using the minimum width, via `ctl_new_pillar_list()`, `ctl_new_pillar()` and ultimately `pillar()` and `pillar_shaft()`
175 1. Determine the number of tiers and the width for each tier
176 1. Distribute the pillars across the tiers, assigning a width to each pillar.
177 1. Format each pillar via its `format()` function, passing the now known width.
178 1. Combine the formatted pillars horizontally.
179 1. Combine the tiers vertically.
180 1. Return the formatted body, and the columns that could not fit.
181
182 In the following, the first and the fourth steps are discussed.
183
184 ## Creating pillar objects
185
186 The initial tibble is passed to `ctl_new_pillar_list()`, which eventually calls `ctl_new_pillar()` once or several times.
187 For each top-level column, one pillar object is constructed.
188 The loop is terminated when the available width is exhausted even considering the minimum width.
189
190
191 ### Pillar lists
192
193 The `ctl_new_pillar_list()` generic dispatches on the container:
194
195 ```{r}
196 ctl_new_pillar_list(tbl, tbl$a, width = 20)
197 ctl_new_pillar_list(tbl, tbl$b, width = 20)
198 ```
199
200 In a tibble, each column can be a data frame, matrix, or even array itself, such columns are called *compound columns*.
201 Such columns are decomposed into sub-pillars and returned as a list of pillars.
202 Regular vectors are forwarded to `ctl_new_pillar()` and returned as list of length one.
203 Implementers of `"tbl"` subclasses will rarely if ever need to extend or override this method.
204
205 ```{r show_source = TRUE}
206 pillar:::ctl_new_pillar_list.tbl
207 ```
208
209
210 ### Simple pillars
211
212 The `ctl_new_pillar()` method is called for columns that are not data frames or arrays, and also dispatches over the container.
213
214 ```{r}
215 ctl_new_pillar(tbl, tbl$a, width = 20)
216 ```
217
218 ```{r show_source = TRUE}
219 pillar:::ctl_new_pillar.tbl
220 ```
221
222 The default method calls `pillar()` directly, passing the maximum width available.
223
224 ```{r show_source = TRUE}
225 pillar
226 ```
227
228 Formatting for title and type is provided by `new_pillar_title()` and `new_pillar_type()`.
229 The body can be customized by implementing `pillar_shaft()` for a vector class, see `vignette("pillar", package = "vctrs")` for details.
230 If title or type don't fit the available width, `pillar_shaft()` is never called.
231
232 This function now returns `NULL` if the width is insufficient to contain the data.
233 It is possible to change the appearance of pillars by overriding or extending `ctl_new_pillar()`.
234
235
236 ### Components
237
238 Pillar objects share the same structure and are ultimately constructed with `new_pillar()`.
239
240 ```{r show_source = TRUE}
241 new_pillar
242 ```
243
244 A pillar is stored as a list of components.
245 Each pillar represents only one simple (atomic) column, compound columns are always represented as multiple pillar objects.
246
247
248 ## Formatting pillars
249
250 When a pillar object is constructed, it has a minimum and a desired (maximum) width.
251 Because it depends on the number and width of other pillar objects that may not be even constructed, the final width is not known yet.
252 It is passed to `format()`, which uses the desired width if empty:
253
254 ```{r show_source = TRUE}
255 pillar:::format.pillar
256 ```