| 1 |
#' Set up formatting |
| 2 |
#' |
| 3 |
#' @description |
| 4 |
#' `tbl_format_setup()` is called by [format.tbl()]. |
| 5 |
#' This method collects information that is common to the header, body, |
| 6 |
#' and footer parts of a tibble. |
| 7 |
#' Examples: |
| 8 |
#' |
| 9 |
#' - the dimensions sometimes are reported both in the header |
| 10 |
#' and (implicitly) in the footer of a tibble; |
| 11 |
#' - the columns shown in the body decide which columns are shown in the footer. |
| 12 |
#' |
| 13 |
#' This information is computed in `tbl_format_setup()`. |
| 14 |
#' The result is passed on to the |
| 15 |
#' [tbl_format_header()], [tbl_format_body()], and [tbl_format_footer()] |
| 16 |
#' methods. |
| 17 |
#' If you need to customize parts of the printed output independently, |
| 18 |
#' override these methods instead. |
| 19 |
#' |
| 20 |
#' By checking the `setup` argument, you can return an object that is |
| 21 |
#' suitable for a call to [tbl_format_header()] if `setup` is `NULL`. |
| 22 |
#' In this case, the method is called a second time with the return value |
| 23 |
#' of the first call as `setup`. |
| 24 |
#' |
| 25 |
#' @details |
| 26 |
#' Extend this method to prepare information that is used |
| 27 |
#' in several parts of the printed output of a tibble-like object, |
| 28 |
#' or to collect additional arguments passed via `...` to |
| 29 |
#' [print.tbl()] or [format.tbl()]. |
| 30 |
#' |
| 31 |
#' We expect that `tbl_format_setup()` is extended only rarely, |
| 32 |
#' and overridden only in exceptional circumstances, if at all. |
| 33 |
#' If you override this method, you must also implement |
| 34 |
#' [tbl_format_header()], [tbl_format_body()], and [tbl_format_footer()] |
| 35 |
#' for your class. |
| 36 |
#' |
| 37 |
#' Implementing a method |
| 38 |
#' allows to override printing and formatting of the entire object |
| 39 |
#' without overriding the [print()] and [format()] methods directly. |
| 40 |
#' This allows to keep the logic of the `width` and `n` arguments. |
| 41 |
#' |
| 42 |
#' @param x |
| 43 |
#' An object. |
| 44 |
#' @param width |
| 45 |
#' Actual width for printing, a numeric greater than zero. |
| 46 |
#' This argument is mandatory for all implementations of this method. |
| 47 |
#' @param ... |
| 48 |
#' Extra arguments to [print.tbl()] or [format.tbl()]. |
| 49 |
#' @param setup |
| 50 |
#' This generic is first called with `setup = NULL` . |
| 51 |
#' If the method _evaluates_ this argument, the return value |
| 52 |
#' will only be used in a call to [tbl_format_header()], |
| 53 |
#' and after that, a second call to this generic will be made |
| 54 |
#' with the return value of the first call as `setup` |
| 55 |
#' which then will be used in calls to [tbl_format_body()] and [tbl_format_footer()]. |
| 56 |
#' This allows displaying the header before starting the computation |
| 57 |
#' required for the body and footer. |
| 58 |
#' @param n |
| 59 |
#' Actual number of rows to print. |
| 60 |
#' No [options][pillar_options] should be considered |
| 61 |
#' by implementations of this method. |
| 62 |
#' @param max_extra_cols |
| 63 |
#' Number of columns to print abbreviated information for, |
| 64 |
#' if the width is too small for the entire tibble. |
| 65 |
#' No [options][pillar_options] should be considered |
| 66 |
#' by implementations of this method. |
| 67 |
#' @param max_footer_lines |
| 68 |
#' Maximum number of lines for the footer. |
| 69 |
#' No [options][pillar_options] should be considered |
| 70 |
#' by implementations of this method. |
| 71 |
#' @param focus `r lifecycle::badge("experimental")` |
| 72 |
#' |
| 73 |
#' Names of columns to show preferentially if space is tight. |
| 74 |
#' @return |
| 75 |
#' An object that can be passed as `setup` argument to |
| 76 |
#' [tbl_format_header()], [tbl_format_body()], and [tbl_format_footer()]. |
| 77 |
#' @export |
| 78 |
#' @examplesIf rlang::is_installed(c("palmerpenguins", "tibble")) |
| 79 |
#' tbl_format_setup(palmerpenguins::penguins) |
| 80 |
tbl_format_setup <- function( |
| 81 |
x, |
| 82 |
width = NULL, |
| 83 |
..., |
| 84 |
setup = list(tbl_sum = tbl_sum(x)), |
| 85 |
n = NULL, |
| 86 |
max_extra_cols = NULL, |
| 87 |
max_footer_lines = NULL, |
| 88 |
focus = NULL |
| 89 |
) { |
| 90 |
"!!!!DEBUG tbl_format_setup()" |
| 91 |
|
| 92 |
width <- get_width_print(width) |
| 93 |
|
| 94 |
n <- get_n_print(n, tbl_nrow(x)) |
| 95 |
|
| 96 |
max_extra_cols <- get_max_extra_cols(max_extra_cols) |
| 97 |
max_footer_lines <- get_max_footer_lines(max_footer_lines) |
| 98 |
|
| 99 |
# Calls UseMethod("tbl_format_setup"), |
| 100 |
# allows using default values in S3 dispatch |
| 101 |
out <- tbl_format_setup_dispatch( |
| 102 |
x, |
| 103 |
width, |
| 104 |
..., |
| 105 |
setup = setup, |
| 106 |
n = n, |
| 107 |
max_extra_cols = max_extra_cols, |
| 108 |
max_footer_lines = max_footer_lines, |
| 109 |
focus = focus |
| 110 |
) |
| 111 |
return(out) |
| 112 |
UseMethod("tbl_format_setup") |
| 113 |
} |
| 114 |
|
| 115 |
tbl_format_setup_dispatch <- function(x, width, ..., n, max_extra_cols, max_footer_lines, focus = NULL) { |
| 116 |
UseMethod("tbl_format_setup") |
| 117 |
} |
| 118 |
|
| 119 |
#' @details |
| 120 |
#' The default method for the `"tbl"` class collects information for |
| 121 |
#' standard printing for tibbles. |
| 122 |
#' See [new_tbl_format_setup()] for details on the returned object. |
| 123 |
#' |
| 124 |
#' @rdname tbl_format_setup |
| 125 |
#' @export |
| 126 |
tbl_format_setup.tbl <- function( |
| 127 |
x, |
| 128 |
width, |
| 129 |
..., |
| 130 |
setup, |
| 131 |
n, |
| 132 |
max_extra_cols, |
| 133 |
max_footer_lines, |
| 134 |
focus |
| 135 |
) { |
| 136 |
"!!!!DEBUG tbl_format_setup.tbl()" |
| 137 |
|
| 138 |
if (is.null(setup)) { |
| 139 |
# Header with early exit |
| 140 |
tbl_sum <- tbl_sum(x) |
| 141 |
return(new_tbl_format_setup(width, tbl_sum, rows_total = NA_integer_)) |
| 142 |
} else { |
| 143 |
tbl_sum <- setup$tbl_sum |
| 144 |
} |
| 145 |
|
| 146 |
# Number of rows |
| 147 |
rows <- tbl_nrow(x) |
| 148 |
|
| 149 |
lazy <- is.na(rows) |
| 150 |
if (lazy) { |
| 151 |
max <- attr(n, "max") %||% n |
| 152 |
df <- as.data.frame(head(x, max + 1)) |
| 153 |
if (nrow(df) <= max) { |
| 154 |
rows <- nrow(df) |
| 155 |
n <- rows |
| 156 |
} else { |
| 157 |
df <- vec_head(df, n) |
| 158 |
} |
| 159 |
} else { |
| 160 |
df <- df_head(x, n) |
| 161 |
} |
| 162 |
|
| 163 |
if (is.na(rows)) { |
| 164 |
# Lazy table with too many rows |
| 165 |
needs_dots <- (nrow(df) >= n) |
| 166 |
} else { |
| 167 |
# Lazy table with few rows, or regular data frame |
| 168 |
needs_dots <- (rows > n) |
| 169 |
} |
| 170 |
|
| 171 |
if (needs_dots) { |
| 172 |
rows_missing <- rows - n |
| 173 |
} else { |
| 174 |
rows_missing <- 0L |
| 175 |
} |
| 176 |
|
| 177 |
# Body |
| 178 |
rownames(df) <- NULL |
| 179 |
|
| 180 |
colonnade <- ctl_colonnade( |
| 181 |
df, |
| 182 |
has_row_id = if (!lazy && .row_names_info(x) > 0) "*" else TRUE, |
| 183 |
width = width, |
| 184 |
controller = x, |
| 185 |
focus = focus |
| 186 |
) |
| 187 |
|
| 188 |
body <- colonnade$body |
| 189 |
|
| 190 |
# Extra columns |
| 191 |
extra_cols <- colonnade$extra_cols |
| 192 |
extra_cols_total <- length(extra_cols) |
| 193 |
|
| 194 |
if (extra_cols_total > max_extra_cols) { |
| 195 |
length(extra_cols) <- max_extra_cols |
| 196 |
} |
| 197 |
|
| 198 |
# Abbreviated columns |
| 199 |
abbrev_cols <- colonnade$abbrev_cols |
| 200 |
|
| 201 |
# Result |
| 202 |
new_tbl_format_setup( |
| 203 |
x = x, |
| 204 |
df = df, |
| 205 |
width = width, |
| 206 |
tbl_sum = tbl_sum, |
| 207 |
body = body, |
| 208 |
rows_missing = rows_missing, |
| 209 |
rows_total = rows, |
| 210 |
extra_cols = extra_cols, |
| 211 |
extra_cols_total = extra_cols_total, |
| 212 |
max_footer_lines = max_footer_lines, |
| 213 |
abbrev_cols = abbrev_cols |
| 214 |
) |
| 215 |
} |
| 216 |
|
| 217 |
#' Number of rows in a tbl object |
| 218 |
#' |
| 219 |
#' This generic will be called by [tbl_format_setup()] to determine the number |
| 220 |
#' of rows in a tbl object. |
| 221 |
#' |
| 222 |
#' @param x A tbl object. |
| 223 |
#' @inheritParams rlang::args_dots_empty |
| 224 |
#' @export |
| 225 |
tbl_nrow <- function(x, ...) { |
| 226 |
check_dots_empty0(...) |
| 227 |
UseMethod("tbl_nrow") |
| 228 |
} |
| 229 |
|
| 230 |
#' @export |
| 231 |
tbl_nrow.tbl <- function(x, ...) { |
| 232 |
nrow(x) |
| 233 |
} |
| 234 |
|
| 235 |
# For glimpse() |
| 236 |
#' @export |
| 237 |
tbl_nrow.data.frame <- tbl_nrow.tbl |
| 238 |
|
| 239 |
|
| 240 |
#' Construct a setup object for formatting |
| 241 |
#' |
| 242 |
#' @description |
| 243 |
#' The object returned from the default method of [tbl_format_setup()] |
| 244 |
#' is an object with a `"class"` attribute and the elements described in the |
| 245 |
#' "Parameters" section. |
| 246 |
#' |
| 247 |
#' Named elements can be added to such objects without affecting the behavior. |
| 248 |
#' Do not modify existing elements. |
| 249 |
#' |
| 250 |
#' @param x The input object unchanged. |
| 251 |
#' @param df A data frame representation of the intended output, |
| 252 |
#' trimmed to the desired number of rows. |
| 253 |
#' @param width The `width` argument unchanged. |
| 254 |
#' @param tbl_sum A named character vector, as returned from [tbl_sum()]. |
| 255 |
#' @param body A character vector with the formatted body, |
| 256 |
#' one element per line, |
| 257 |
#' @param rows_missing The number of rows not shown from the body, |
| 258 |
#' `NA` if unknown. |
| 259 |
#' @param rows_total The total number of rows in the data, |
| 260 |
#' `NA` if unknown. |
| 261 |
#' @param extra_cols Columns that did not fit into the body, |
| 262 |
#' as a character vector of formatted column names and types. |
| 263 |
#' @param extra_cols_total The total number of columns, may be larger than |
| 264 |
#' `length(extra_cols)`. |
| 265 |
#' @param max_footer_lines The maximum number of lines in the footer. |
| 266 |
#' @param abbrev_cols Formatted names of the columns that are shown abbreviated |
| 267 |
#' in the body. |
| 268 |
#' |
| 269 |
#' @keywords internal |
| 270 |
new_tbl_format_setup <- function( |
| 271 |
width, |
| 272 |
tbl_sum, |
| 273 |
x = NULL, |
| 274 |
df = NULL, |
| 275 |
body = NULL, |
| 276 |
rows_missing = NULL, |
| 277 |
rows_total = NULL, |
| 278 |
extra_cols = NULL, |
| 279 |
extra_cols_total = NULL, |
| 280 |
max_footer_lines = NULL, |
| 281 |
abbrev_cols = NULL |
| 282 |
) { |
| 283 |
trunc_info <- list( |
| 284 |
x = x, |
| 285 |
df = df, |
| 286 |
width = width, |
| 287 |
tbl_sum = tbl_sum, |
| 288 |
body = body, |
| 289 |
rows_missing = rows_missing, |
| 290 |
rows_total = rows_total, |
| 291 |
extra_cols = extra_cols, |
| 292 |
extra_cols_total = extra_cols_total, |
| 293 |
max_footer_lines = max_footer_lines, |
| 294 |
abbrev_cols = abbrev_cols |
| 295 |
) |
| 296 |
|
| 297 |
structure(trunc_info, class = "pillar_tbl_format_setup") |
| 298 |
} |
| 299 |
|
| 300 |
is_tbl_format_setup <- function(x) { |
| 301 |
inherits(x, "pillar_tbl_format_setup") |
| 302 |
} |
| 303 |
|
| 304 |
# For testing |
| 305 |
#' @export |
| 306 |
print.pillar_tbl_format_setup <- function(x, ...) { |
| 307 |
writeLines(format(x, ...)) |
| 308 |
invisible(x) |
| 309 |
} |
| 310 |
|
| 311 |
#' @export |
| 312 |
format.pillar_tbl_format_setup <- function(x, ...) { |
| 313 |
check_dots_empty() |
| 314 |
c( |
| 315 |
cli::style_bold("<pillar_tbl_format_setup>"), |
| 316 |
tbl_format_header(x), |
| 317 |
tbl_format_body(x), |
| 318 |
tbl_format_footer(x) |
| 319 |
) |
| 320 |
} |