| 1 |
#' Format the header 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_header()` method is responsible for formatting the header |
| 9 |
#' of a tibble. |
| 10 |
#' |
| 11 |
#' Override this method if you need to change the appearance |
| 12 |
#' of the entire header. |
| 13 |
#' If you only need to change or extend the components shown in the header, |
| 14 |
#' override or extend [tbl_sum()] for your class which is called by the |
| 15 |
#' default method. |
| 16 |
#' |
| 17 |
#' @inheritParams tbl_format_body |
| 18 |
#' @inherit tbl_format_body return |
| 19 |
#' |
| 20 |
#' @export |
| 21 |
#' @examplesIf rlang::is_installed(c("palmerpenguins", "tibble")) |
| 22 |
#' setup <- tbl_format_setup(palmerpenguins::penguins) |
| 23 |
#' tbl_format_header(palmerpenguins::penguins, setup) |
| 24 |
#' |
| 25 |
#' # Shortcut for debugging |
| 26 |
#' tbl_format_header(setup) |
| 27 |
tbl_format_header <- function(x, setup, ...) { |
| 28 |
check_dots_empty() |
| 29 |
|
| 30 |
UseMethod("tbl_format_header") |
| 31 |
} |
| 32 |
|
| 33 |
#' @export |
| 34 |
tbl_format_header.tbl <- function(x, setup, ...) { |
| 35 |
named_header <- setup$tbl_sum |
| 36 |
focus <- attr(x, "pillar_focus") |
| 37 |
if (!is.null(focus)) { |
| 38 |
named_header <- c(named_header, "Focus columns" = collapse(tick_if_needed(focus))) |
| 39 |
} |
| 40 |
|
| 41 |
if (all(names2(named_header) == "")) { |
| 42 |
header <- named_header |
| 43 |
} else { |
| 44 |
header <- paste0( |
| 45 |
align(paste0(names2(named_header), ":"), space = NBSP), |
| 46 |
# We add a space after the NBSP inserted by align() |
| 47 |
# so that wrapping occurs at the right location for very narrow outputs |
| 48 |
" ", |
| 49 |
named_header |
| 50 |
) |
| 51 |
} |
| 52 |
|
| 53 |
style_subtle(format_comment(header, width = setup$width)) |
| 54 |
} |
| 55 |
|
| 56 |
#' @export |
| 57 |
tbl_format_header.pillar_tbl_format_setup <- function(x, ...) { |
| 58 |
as_glue(c( |
| 59 |
cli::style_bold("<tbl_format_header(setup)>"), |
| 60 |
tbl_format_header(x$x, setup = x) |
| 61 |
)) |
| 62 |
} |