Debian Codegraph @

R/ctl_pillar.R

raw
1 #' Object for formatting a vector suitable for tabular display
2 #'
3 #' `pillar()` creates an object that formats a vector.
4 #' The output uses one row for a title (if given), one row for the type,
5 #' and `vec_size(x)` rows for the data.
6 #'
7 #' A pillar consists of arbitrary components.
8 #' The `pillar()` constructor uses `title`, `type`, and `data`.
9 #'
10 #' - `title` via [new_pillar_title()]
11 #' - `type` via [new_pillar_type()], which calls [type_sum()]
12 #' internally
13 #' - `data` via [pillar_shaft()]
14 #'
15 #' All components are formatted via [format()] when displaying the pillar.
16 #' A `width` argument is passed to each `format()` call.
17 #'
18 #' As of pillar 1.5.0, `pillar()` returns `NULL` if the width is insufficient
19 #' to display the data.
20 #'
21 #' @param x A vector to format.
22 #' @param title An optional title for the column. The title will be
23 #' used "as is", no quoting will be applied.
24 #' @param width Default width, optional.
25 #' @param ... Passed on to [pillar_shaft()].
26 #' @export
27 #' @examples
28 #' x <- 123456789 * (10^c(-1, -3, -5, NA, -8, -10))
29 #' pillar(x)
30 #' pillar(-x)
31 #' pillar(runif(10))
32 #' pillar(rcauchy(20))
33 #'
34 #' # Special values are highlighted
35 #' pillar(c(runif(5), NA, NaN, Inf, -Inf))
36 #'
37 #' # Very wide ranges will be displayed in scientific format
38 #' pillar(c(1e10, 1e-10), width = 20)
39 #' pillar(c(1e10, 1e-10))
40 #'
41 #' x <- c(FALSE, NA, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE, FALSE, TRUE)
42 #' pillar(x)
43 #'
44 #' x <- c("This is string is rather long", NA, "?", "Short")
45 #' pillar(x)
46 #' pillar(x, width = 30)
47 #' pillar(x, width = 5)
48 #'
49 #' date <- as.Date("2017-05-15")
50 #' pillar(date + c(1, NA, 3:5))
51 #' pillar(as.POSIXct(date) + c(30, NA, 600, 3600, 86400))
52 pillar <- function(x, title = NULL, width = NULL, ...) {
53 "!!!!DEBUG pillar(`v(class(x))`, `v(title)`, `v(width)`)"
54
55 pillar_from_shaft(
56 new_pillar_title(title),
57 new_pillar_type(x),
58 pillar_shaft(x, ...),
59 width
60 )
61 }
62
63 pillar_from_shaft <- function(title, type, data, width) {
64 "!!!!!DEBUG pillar_from_shaft(`v(width)`)"
65
66 if (is.null(width)) {
67 my_width <- Inf
68 } else {
69 my_width <- width
70 }
71
72 if (get_min_width(title) > my_width) {
73 "!!!!!DEBUG Early exit, (`v(get_min_width(title))`) > (`v(my_width)`)"
74 return(NULL)
75 }
76
77 data_min_width <- get_min_width(data)
78 if (data_min_width > my_width) {
79 "!!!!!DEBUG Early exit, (`v(get_min_width(data))`) > (`v(my_width)`)"
80 return(NULL)
81 }
82 data_width <- get_width(data)
83
84 data_component <- new_pillar_component(list(data), width = data_width, min_width = data_min_width)
85
86 # The type is taken either from the shaft (e.g. in the case of num() with
87 # common exponents) or from the type provided by the caller
88 shaft_type <- attr(data, "type", exact = TRUE)
89 if (!is.null(shaft_type)) {
90 type <- shaft_type
91 } else {
92 force(type)
93 }
94
95 # Delay querying width of type until it is decided if we take the type_sum
96 # from the data or from the argument provided
97 if (get_min_width(type) > my_width) {
98 "!!!!!DEBUG Early exit, (`v(get_min_width(type))`) > (`v(my_width)`)"
99 return(NULL)
100 }
101
102 new_pillar(
103 list(
104 title = pillar_component(title),
105 type = pillar_component(type),
106 data = data_component
107 ),
108 width = width
109 )
110 }
111
112 #' Construct a custom pillar object
113 #'
114 #' @description
115 #' `r lifecycle::badge("experimental")`
116 #'
117 #' `new_pillar()` is the low-level constructor for pillar objects.
118 #' It supports arbitrary components.
119 #' See [pillar()] for the high-level constructor with default components.
120 #'
121 #' @details
122 #' Arbitrary components are supported.
123 #' If your tibble subclass needs more or different components in its pillars,
124 #' override or extend [ctl_new_pillar()] and perhaps [ctl_new_pillar_list()].
125 #'
126 #' @inheritParams rlang::args_dots_empty
127 #' @inheritParams pillar
128 #' @param components A named list of components constructed with [pillar_component()].
129 #' @param class Name of subclass.
130 #' @param extra Deprecated.
131 #'
132 #' @export
133 #' @examples
134 #' lines <- function(char = "-") {
135 #' stopifnot(nchar(char) == 1)
136 #' structure(char, class = "lines")
137 #' }
138 #'
139 #' format.lines <- function(x, width, ...) {
140 #' paste(rep(x, width), collapse = "")
141 #' }
142 #'
143 #' new_pillar(list(
144 #' title = pillar_component(new_ornament(c("abc", "de"), align = "right")),
145 #' lines = new_pillar_component(list(lines("=")), width = 1)
146 #' ))
147 new_pillar <- function(components, ..., width = NULL, class = NULL,
148 extra = deprecated()) {
149 "!!!!DEBUG new_pillar(`v(width)`, `v(class)`)"
150
151 if (is_present(extra)) {
152 # Signal the deprecation to the user
153 deprecate_warn("1.7.0", "pillar::new_pillar(extra = )")
154 }
155
156 check_dots_empty()
157 if (length(components) > 0 && !is_named(components)) {
158 abort("All components must have names.")
159 }
160
161 structure(
162 components,
163 width = width,
164 class = c(class, "pillar")
165 )
166 }
167
168 #' @export
169 format.pillar <- function(x, width = NULL, ...) {
170 if (is.null(width)) {
171 width <- get_width(x)
172 }
173
174 if (is.null(width)) {
175 width <- pillar_get_width(x)
176 }
177
178 as_glue(pillar_format_parts_2(x, width)$aligned)
179 }
180
181 #' @export
182 print.pillar <- function(x, ...) {
183 writeLines(style_bold("<pillar>"))
184 print(format(x, ...))
185 }