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