| 1 |
#' Format the footer of a tibble |
| 2 |
#' |
| 3 |
#' @description |
| 4 |
#' `r lifecycle::badge("experimental")` |
| 5 |
#' |
| 6 |
#' For easier customization, the formatting of a tibble is split |
| 7 |
#' into three components: header, body, and footer. |
| 8 |
#' The `tbl_format_footer()` method is responsible for formatting the footer |
| 9 |
#' of a tibble. |
| 10 |
#' |
| 11 |
#' Override or extend this method if you need to change the appearance |
| 12 |
#' of the footer. |
| 13 |
#' The default implementation adds information about rows and columns |
| 14 |
#' that are not shown in the body. |
| 15 |
#' |
| 16 |
#' @inheritParams tbl_format_body |
| 17 |
#' @inherit tbl_format_body return |
| 18 |
#' |
| 19 |
#' @export |
| 20 |
#' @examplesIf rlang::is_installed(c("palmerpenguins", "tibble")) |
| 21 |
#' setup <- tbl_format_setup(palmerpenguins::penguins) |
| 22 |
#' tbl_format_footer(palmerpenguins::penguins, setup) |
| 23 |
#' |
| 24 |
#' # Shortcut for debugging |
| 25 |
#' tbl_format_footer(setup) |
| 26 |
tbl_format_footer <- function(x, setup, ...) { |
| 27 |
check_dots_empty() |
| 28 |
|
| 29 |
UseMethod("tbl_format_footer") |
| 30 |
} |
| 31 |
|
| 32 |
#' @export |
| 33 |
tbl_format_footer.pillar_tbl_format_setup <- function(x, ...) { |
| 34 |
as_glue(c( |
| 35 |
cli::style_bold("<tbl_format_footer(setup)>"), |
| 36 |
tbl_format_footer(x$x, setup = x) |
| 37 |
)) |
| 38 |
} |
| 39 |
|
| 40 |
#' @export |
| 41 |
tbl_format_footer.tbl <- function(x, setup, ...) { |
| 42 |
footer <- format_footer(x, setup) |
| 43 |
footer_comment <- wrap_footer_bullet(footer, setup) |
| 44 |
footer_advice <- format_footer_advice(x, setup) |
| 45 |
footer_advice_comment <- wrap_footer_bullet( |
| 46 |
footer_advice, |
| 47 |
setup, |
| 48 |
lines = 1, |
| 49 |
ellipsis = FALSE, |
| 50 |
bullet = symbol$info |
| 51 |
) |
| 52 |
style_subtle(c(footer_comment, footer_advice_comment)) |
| 53 |
} |
| 54 |
|
| 55 |
format_footer <- function(x, setup) { |
| 56 |
extra_rows <- format_footer_extra_rows(x, setup) |
| 57 |
abbrev_cols <- format_footer_abbrev_cols(x, setup) |
| 58 |
extra_cols <- format_footer_extra_cols(x, setup) |
| 59 |
|
| 60 |
compact(list(extra_rows, abbrev_cols, extra_cols)) |
| 61 |
} |
| 62 |
|
| 63 |
format_footer_extra_rows <- function(x, setup) { |
| 64 |
if (length(setup$df) != 0) { |
| 65 |
if (is.na(setup$rows_missing)) { |
| 66 |
c("more", "rows") |
| 67 |
} else if (setup$rows_missing > 0) { |
| 68 |
c(big_mark(setup$rows_missing), "more", pluralise_n("row(s)", setup$rows_missing)) |
| 69 |
} |
| 70 |
} else { |
| 71 |
rows_body <- nrow(setup$df) |
| 72 |
if (is.na(setup$rows_total) && rows_body > 0) { |
| 73 |
c("at", "least", big_mark(rows_body), pluralise_n("row(s)", rows_body), "total") |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
format_footer_abbrev_cols <- function(x, setup) { |
| 79 |
abbrev_cols <- setup$abbrev_cols |
| 80 |
abbrev_cols_total <- length(abbrev_cols) |
| 81 |
if (abbrev_cols_total == 0) { |
| 82 |
return(NULL) |
| 83 |
} |
| 84 |
|
| 85 |
abbrev_cols <- paste0( |
| 86 |
map_chr(seq_along(abbrev_cols), superdigit), |
| 87 |
pillar_options$superdigit_sep(), |
| 88 |
abbrev_cols |
| 89 |
) |
| 90 |
|
| 91 |
idx_all_but_last <- seq_len(abbrev_cols_total - 1) |
| 92 |
abbrev_cols[idx_all_but_last] <- paste0(abbrev_cols[idx_all_but_last], ",") |
| 93 |
|
| 94 |
c( |
| 95 |
"abbreviated", |
| 96 |
pluralise("name(s):", abbrev_cols), |
| 97 |
abbrev_cols |
| 98 |
) |
| 99 |
} |
| 100 |
|
| 101 |
format_footer_extra_cols <- function(x, setup) { |
| 102 |
extra_cols <- setup$extra_cols |
| 103 |
if (length(extra_cols) == 0) { |
| 104 |
return(NULL) |
| 105 |
} |
| 106 |
|
| 107 |
extra_cols_total <- setup$extra_cols_total |
| 108 |
|
| 109 |
vars <- format_extra_vars(extra_cols, extra_cols_total) |
| 110 |
c( |
| 111 |
big_mark(extra_cols_total), |
| 112 |
if (!identical(setup$rows_total, 0L) && nrow(setup$df) > 0) "more", |
| 113 |
pluralise_n("variable(s):", extra_cols_total), |
| 114 |
vars |
| 115 |
) |
| 116 |
} |
| 117 |
|
| 118 |
format_extra_vars <- function(extra_cols, extra_cols_total) { |
| 119 |
out <- unname(map2_chr(extra_cols, names(extra_cols), format_abbrev)) |
| 120 |
|
| 121 |
if (extra_cols_total > length(extra_cols)) { |
| 122 |
out <- c(out, symbol$ellipsis) |
| 123 |
} |
| 124 |
|
| 125 |
out[-length(out)] <- paste0(out[-length(out)], ",") |
| 126 |
out |
| 127 |
} |
| 128 |
|
| 129 |
format_footer_advice <- function(x, setup) { |
| 130 |
if (!isTRUE(pillar_options$advice())) { |
| 131 |
return() |
| 132 |
} |
| 133 |
|
| 134 |
if (setup$extra_cols_total > length(setup$extra_cols)) { |
| 135 |
cols <- "`colnames()` to see all variable names" |
| 136 |
} else { |
| 137 |
cols <- NULL |
| 138 |
} |
| 139 |
|
| 140 |
if (is.na(setup$rows_missing) || setup$rows_missing > 0) { |
| 141 |
rows <- "`print(n = ...)` to see more rows" |
| 142 |
} else { |
| 143 |
rows <- NULL |
| 144 |
} |
| 145 |
|
| 146 |
advice <- enum_collapse(c(rows, cols)) |
| 147 |
|
| 148 |
if (length(advice) == 0) { |
| 149 |
return() |
| 150 |
} |
| 151 |
|
| 152 |
paste0("Use ", paste(advice, collapse = " ")) |
| 153 |
} |
| 154 |
|
| 155 |
wrap_footer_bullet <- function(footers, |
| 156 |
setup, |
| 157 |
lines = setup$max_footer_lines, |
| 158 |
ellipsis = TRUE, |
| 159 |
bullet = symbol$info) { |
| 160 |
out <- character() |
| 161 |
|
| 162 |
for (footer in footers) { |
| 163 |
if (lines <= 0) { |
| 164 |
break |
| 165 |
} |
| 166 |
current <- wrap_footer_bullet_one(footer, setup, lines, ellipsis, bullet) |
| 167 |
out <- c(out, current) |
| 168 |
lines <- lines - length(current) |
| 169 |
} |
| 170 |
|
| 171 |
out |
| 172 |
} |
| 173 |
|
| 174 |
wrap_footer_bullet_one <- function(footer, |
| 175 |
setup, |
| 176 |
lines, |
| 177 |
ellipsis, |
| 178 |
bullet) { |
| 179 |
# When asking for width = 80, use at most 79 characters |
| 180 |
max_extent <- setup$width - 1L |
| 181 |
|
| 182 |
tier_widths <- get_footer_tier_widths_bullets( |
| 183 |
footer, max_extent, lines, ellipsis |
| 184 |
) |
| 185 |
|
| 186 |
# show optuput even if too wide |
| 187 |
widths <- pmin(get_extent(footer), max_extent - 4L) |
| 188 |
wrap <- colonnade_compute_tiered_col_widths_df(widths, widths, tier_widths) |
| 189 |
|
| 190 |
# truncate output that doesn't fit |
| 191 |
truncated <- anyNA(wrap$tier) |
| 192 |
split <- split(footer[wrap$id], wrap$tier) |
| 193 |
if (ellipsis && truncated && length(split) > 0) { |
| 194 |
split[[length(split)]] <- c(split[[length(split)]], symbol$ellipsis) |
| 195 |
} |
| 196 |
split <- imap(split, function(.x, .y) { |
| 197 |
if (.y == 1) { |
| 198 |
header <- bullet |
| 199 |
} else { |
| 200 |
header <- " " |
| 201 |
} |
| 202 |
c("#", header, .x) |
| 203 |
}) |
| 204 |
|
| 205 |
map_chr(split, paste, collapse = " ") |
| 206 |
} |
| 207 |
|
| 208 |
get_footer_tier_widths_bullets <- function(footer, max_extent, n_tiers, ellipsis = TRUE) { |
| 209 |
if (ellipsis) { |
| 210 |
extra_width <- get_extent(symbol$ellipsis) + 1L # space, ellipsis |
| 211 |
} else { |
| 212 |
extra_width <- FALSE |
| 213 |
} |
| 214 |
|
| 215 |
n_tiers <- min(length(footer), n_tiers) |
| 216 |
|
| 217 |
c( |
| 218 |
rep(max_extent - 4, n_tiers - 1), |
| 219 |
max_extent - 4 - extra_width |
| 220 |
) |
| 221 |
} |
| 222 |
|
| 223 |
pre_dots <- function(x) { |
| 224 |
if (length(x) > 0) { |
| 225 |
paste0(symbol$ellipsis, " ", x) |
| 226 |
} else { |
| 227 |
character() |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
collapse <- function(x) paste(x, collapse = ", ") |
| 232 |
|
| 233 |
split_lines <- function(x) { |
| 234 |
# Avoid .ptype argument to vec_c() |
| 235 |
if (is_empty(x)) { |
| 236 |
return(character()) |
| 237 |
} |
| 238 |
|
| 239 |
unlist(strsplit(x, "\n", fixed = TRUE)) |
| 240 |
} |
| 241 |
|
| 242 |
enum_collapse <- function(x) { |
| 243 |
if (length(x) > 1) { |
| 244 |
x_len <- length(x) |
| 245 |
idx_all_but_last <- seq2(1, x_len - 1) |
| 246 |
x[idx_all_but_last] <- map(x[idx_all_but_last], function(x) { |
| 247 |
x[[length(x)]] <- paste0(x[[length(x)]], ",") |
| 248 |
x |
| 249 |
}) |
| 250 |
x <- c(x[idx_all_but_last], "and", x[x_len]) |
| 251 |
} |
| 252 |
|
| 253 |
x |
| 254 |
} |