Debian Codegraph @

inst/doc/extending.R

raw
1 ## ----include = FALSE----------------------------------------------------------
2 knitr::opts_chunk$set(
3 collapse = TRUE,
4 comment = "#>"
5 )
6
7 ## ----setup--------------------------------------------------------------------
8 library(pillar)
9
10 ## -----------------------------------------------------------------------------
11 example_tbl <- function(class = NULL) {
12 vctrs::new_data_frame(
13 list(
14 a = letters[1:3],
15 b = data.frame(c = 1:3, d = 4:6 + 0.5)
16 ),
17 class = c(class, "tbl")
18 )
19 }
20
21 ## -----------------------------------------------------------------------------
22 example_tbl()
23
24 ## -----------------------------------------------------------------------------
25 tbl_sum.default_header_extend <- function(x, ...) {
26 default_header <- NextMethod()
27 c(default_header, "New" = "A new header")
28 }
29
30 example_tbl("default_header_extend")
31
32 tbl_sum.default_header_replace <- function(x, ...) {
33 c("Override" = "Replace all headers")
34 }
35
36 example_tbl("default_header_replace")
37
38 ## -----------------------------------------------------------------------------
39 tbl_format_header.custom_header_replace <- function(x, setup, ...) {
40 cli::style_italic(names(setup$tbl_sum), " = ", setup$tbl_sum)
41 }
42
43 example_tbl("custom_header_replace")
44
45 ## -----------------------------------------------------------------------------
46 tbl_format_footer.custom_footer_extend <- function(x, setup, ...) {
47 default_footer <- NextMethod()
48
49 extra_info <- "and with extra info in the footer"
50 extra_footer <- style_subtle(paste0("# ", cli::symbol$ellipsis, " ", extra_info))
51
52 c(default_footer, extra_footer)
53 }
54
55 print(example_tbl("custom_footer_extend"), n = 2)
56
57 tbl_format_footer.custom_footer_replace <- function(x, setup, ...) {
58 paste0("The table has ", setup$rows_total, " rows in total.")
59 }
60
61 print(example_tbl("custom_footer_replace"), n = 2)
62
63 ## -----------------------------------------------------------------------------
64 tbl_format_setup.extra_info <- function(x, width, ...) {
65 setup <- NextMethod()
66 cells <- prod(dim(x))
67 setup$cells <- cells
68 setup$tbl_sum <- c(setup$tbl_sum, "Cells" = as.character(cells))
69 setup
70 }
71
72 tbl_format_footer.extra_info <- function(x, setup, ...) {
73 paste0("The table has ", setup$cells, " cells in total.")
74 }
75
76 example_tbl("extra_info")
77
78 ## -----------------------------------------------------------------------------
79 ctl_new_rowid_pillar.pillar_roman <- function(controller, x, width, ...) {
80 out <- NextMethod()
81 rowid <- utils::as.roman(seq_len(nrow(x)))
82 width <- max(nchar(as.character(rowid)))
83 new_pillar(
84 list(
85 title = out$title,
86 type = out$type,
87 data = pillar_component(
88 new_pillar_shaft(list(row_ids = rowid),
89 width = width,
90 class = "pillar_rif_shaft"
91 )
92 )
93 ),
94 width = width
95 )
96 }
97
98 example_tbl("pillar_roman")
99
100 ## -----------------------------------------------------------------------------
101 ctl_new_pillar.pillar_rule <- function(controller, x, width, ..., title = NULL) {
102 out <- NextMethod()
103 new_pillar(list(
104 top_rule = new_pillar_component(list("========"), width = 8),
105 title = out$title,
106 type = out$type,
107 mid_rule = new_pillar_component(list("--------"), width = 8),
108 data = out$data,
109 bottom_rule = new_pillar_component(list("========"), width = 8)
110 ))
111 }
112
113 example_tbl("pillar_rule")
114
115 ## -----------------------------------------------------------------------------
116 rule <- function(char = "-") {
117 stopifnot(nchar(char) == 1)
118 structure(char, class = "rule")
119 }
120
121 format.rule <- function(x, width, ...) {
122 paste(rep(x, width), collapse = "")
123 }
124
125 ctl_new_pillar.pillar_rule_adaptive <- function(controller, x, width, ..., title = NULL) {
126 out <- NextMethod()
127 if (is.null(out)) {
128 return(NULL)
129 }
130
131 new_pillar(list(
132 top_rule = new_pillar_component(list(rule("=")), width = 1),
133 title = out$title,
134 type = out$type,
135 mid_rule = new_pillar_component(list(rule("-")), width = 1),
136 data = out$data,
137 bottom_rule = new_pillar_component(list(rule("=")), width = 1)
138 ))
139 }
140
141 example_tbl("pillar_rule_adaptive")
142
143 ## -----------------------------------------------------------------------------
144 ctl_new_pillar_list.hide_df <- function(controller, x, width, ..., title = NULL) {
145 if (!is.data.frame(x)) {
146 return(NextMethod())
147 }
148
149 if (width < 8) {
150 return(NULL)
151 }
152
153 list(new_pillar(
154 list(
155 title = pillar_component(new_pillar_title(title)),
156 type = new_pillar_component(list("<hidden>"), width = 8),
157 data = new_pillar_component(list(""), width = 1)
158 ),
159 width = 8
160 ))
161 }
162
163 example_tbl("hide_df")
164
165 ## -----------------------------------------------------------------------------
166 tbl_format_body.oldskool <- function(x, setup, ...) {
167 capture.output(print.data.frame(setup$df))
168 }
169
170 print(example_tbl("oldskool"), n = 2)
171