| 1 |
## ----include = FALSE---------------------------------------------------------- |
| 2 |
knitr::opts_chunk$set( |
| 3 |
collapse = TRUE, |
| 4 |
eval = rlang::is_installed("tibble"), |
| 5 |
comment = "#>" |
| 6 |
) |
| 7 |
|
| 8 |
pillar:::set_show_source_hooks() |
| 9 |
|
| 10 |
## ----setup-------------------------------------------------------------------- |
| 11 |
library(pillar) |
| 12 |
|
| 13 |
## ----echo = FALSE, error = TRUE----------------------------------------------- |
| 14 |
try({ |
| 15 |
text <- paste( |
| 16 |
readLines("format.mmd"), |
| 17 |
collapse = "\n" |
| 18 |
) |
| 19 |
DiagrammeR::mermaid(text) |
| 20 |
}) |
| 21 |
|
| 22 |
## ----------------------------------------------------------------------------- |
| 23 |
tbl <- tibble::tibble(a = 1:3, b = tibble::tibble(c = 4:6, d = 7:9), e = 10:12) |
| 24 |
print(tbl, width = 23) |
| 25 |
str(tbl) |
| 26 |
|
| 27 |
## ----show_source = TRUE------------------------------------------------------- |
| 28 |
# print.tbl <- function (x, width = NULL, ..., n = NULL, max_extra_cols = NULL, |
| 29 |
# max_footer_lines = NULL) |
| 30 |
# { |
| 31 |
# print_tbl(x, width, ..., n = n, max_extra_cols = max_extra_cols, |
| 32 |
# max_footer_lines = max_footer_lines) |
| 33 |
# } |
| 34 |
|
| 35 |
## ----show_source = TRUE------------------------------------------------------- |
| 36 |
# format.tbl <- function (x, width = NULL, ..., n = NULL, max_extra_cols = NULL, |
| 37 |
# max_footer_lines = NULL) |
| 38 |
# { |
| 39 |
# format_tbl(x, width, ..., n = n, max_extra_cols = max_extra_cols, |
| 40 |
# max_footer_lines = max_footer_lines) |
| 41 |
# } |
| 42 |
|
| 43 |
## ----------------------------------------------------------------------------- |
| 44 |
setup <- tbl_format_setup(tbl, width = 24) |
| 45 |
setup |
| 46 |
|
| 47 |
## ----show_source = TRUE------------------------------------------------------- |
| 48 |
# tbl_format_setup <- function (x, width = NULL, ..., setup = list(tbl_sum = tbl_sum(x)), |
| 49 |
# n = NULL, max_extra_cols = NULL, max_footer_lines = NULL, |
| 50 |
# focus = NULL) |
| 51 |
# { |
| 52 |
# "!!!!DEBUG tbl_format_setup()" |
| 53 |
# width <- get_width_print(width) |
| 54 |
# n <- get_n_print(n, tbl_nrow(x)) |
| 55 |
# max_extra_cols <- get_max_extra_cols(max_extra_cols) |
| 56 |
# max_footer_lines <- get_max_footer_lines(max_footer_lines) |
| 57 |
# out <- tbl_format_setup_dispatch(x, width, ..., setup = setup, |
| 58 |
# n = n, max_extra_cols = max_extra_cols, max_footer_lines = max_footer_lines, |
| 59 |
# focus = focus) |
| 60 |
# return(out) |
| 61 |
# UseMethod("tbl_format_setup") |
| 62 |
# } |
| 63 |
|
| 64 |
## ----show_source = TRUE------------------------------------------------------- |
| 65 |
# tbl_format_setup.tbl <- function (x, width, ..., setup, n, max_extra_cols, max_footer_lines, |
| 66 |
# focus) |
| 67 |
# { |
| 68 |
# "!!!!DEBUG tbl_format_setup.tbl()" |
| 69 |
# if (is.null(setup)) { |
| 70 |
# tbl_sum <- tbl_sum(x) |
| 71 |
# return(new_tbl_format_setup(width, tbl_sum, rows_total = NA)) |
| 72 |
# } |
| 73 |
# else { |
| 74 |
# tbl_sum <- setup$tbl_sum |
| 75 |
# } |
| 76 |
# rows <- tbl_nrow(x) |
| 77 |
# lazy <- is.na(rows) |
| 78 |
# if (lazy) { |
| 79 |
# max <- attr(n, "max") %||% n |
| 80 |
# df <- as.data.frame(head(x, max + 1)) |
| 81 |
# if (nrow(df) <= max) { |
| 82 |
# rows <- nrow(df) |
| 83 |
# n <- rows |
| 84 |
# } |
| 85 |
# else { |
| 86 |
# df <- vec_head(df, n) |
| 87 |
# } |
| 88 |
# } |
| 89 |
# else { |
| 90 |
# df <- df_head(x, n) |
| 91 |
# } |
| 92 |
# if (is.na(rows)) { |
| 93 |
# needs_dots <- (nrow(df) >= n) |
| 94 |
# } |
| 95 |
# else { |
| 96 |
# needs_dots <- (rows > n) |
| 97 |
# } |
| 98 |
# if (needs_dots) { |
| 99 |
# rows_missing <- rows - n |
| 100 |
# } |
| 101 |
# else { |
| 102 |
# rows_missing <- 0 |
| 103 |
# } |
| 104 |
# rownames(df) <- NULL |
| 105 |
# colonnade <- ctl_colonnade(df, has_row_id = if (!lazy && |
| 106 |
# .row_names_info(x) > 0) |
| 107 |
# "*" |
| 108 |
# else TRUE, width = width, controller = x, focus = focus) |
| 109 |
# body <- colonnade$body |
| 110 |
# extra_cols <- colonnade$extra_cols |
| 111 |
# extra_cols_total <- length(extra_cols) |
| 112 |
# if (extra_cols_total > max_extra_cols) { |
| 113 |
# length(extra_cols) <- max_extra_cols |
| 114 |
# } |
| 115 |
# abbrev_cols <- colonnade$abbrev_cols |
| 116 |
# new_tbl_format_setup(x = x, df = df, width = width, tbl_sum = tbl_sum, |
| 117 |
# body = body, rows_missing = rows_missing, rows_total = rows, |
| 118 |
# extra_cols = extra_cols, extra_cols_total = extra_cols_total, |
| 119 |
# max_footer_lines = max_footer_lines, abbrev_cols = abbrev_cols) |
| 120 |
# } |
| 121 |
|
| 122 |
## ----------------------------------------------------------------------------- |
| 123 |
tbl_format_header(tbl, setup) |
| 124 |
tbl_format_body(tbl, setup) |
| 125 |
tbl_format_footer(tbl, setup) |
| 126 |
|
| 127 |
## ----------------------------------------------------------------------------- |
| 128 |
class(tbl_format_body(tbl, setup)) |
| 129 |
typeof(tbl_format_body(tbl, setup)) |
| 130 |
|
| 131 |
## ----show_source = TRUE------------------------------------------------------- |
| 132 |
# tbl_format_header.tbl <- function (x, setup, ...) |
| 133 |
# { |
| 134 |
# named_header <- setup$tbl_sum |
| 135 |
# focus <- attr(x, "pillar_focus") |
| 136 |
# if (!is.null(focus)) { |
| 137 |
# named_header <- c(named_header, `Focus columns` = collapse(tick_if_needed(focus))) |
| 138 |
# } |
| 139 |
# if (all(names2(named_header) == "")) { |
| 140 |
# header <- named_header |
| 141 |
# } |
| 142 |
# else { |
| 143 |
# header <- paste0(align(paste0(names2(named_header), ":"), |
| 144 |
# space = NBSP), " ", named_header) |
| 145 |
# } |
| 146 |
# style_subtle(format_comment(header, width = setup$width)) |
| 147 |
# } |
| 148 |
|
| 149 |
## ----show_source = TRUE------------------------------------------------------- |
| 150 |
# tbl_format_body.tbl <- function (x, setup, ...) |
| 151 |
# { |
| 152 |
# force(setup) |
| 153 |
# setup$body |
| 154 |
# } |
| 155 |
|
| 156 |
## ----show_source = TRUE------------------------------------------------------- |
| 157 |
# tbl_format_footer.tbl <- function (x, setup, ...) |
| 158 |
# { |
| 159 |
# footer <- format_footer(x, setup) |
| 160 |
# footer_comment <- wrap_footer_bullet(footer, setup) |
| 161 |
# footer_advice <- format_footer_advice(x, setup) |
| 162 |
# footer_advice_comment <- wrap_footer_bullet(footer_advice, |
| 163 |
# setup, lines = 1, ellipsis = FALSE, bullet = symbol$info) |
| 164 |
# style_subtle(c(footer_comment, footer_advice_comment)) |
| 165 |
# } |
| 166 |
|
| 167 |
## ----------------------------------------------------------------------------- |
| 168 |
ctl_new_pillar_list(tbl, tbl$a, width = 20) |
| 169 |
ctl_new_pillar_list(tbl, tbl$b, width = 20) |
| 170 |
|
| 171 |
## ----show_source = TRUE------------------------------------------------------- |
| 172 |
# ctl_new_pillar_list.tbl <- function (controller, x, width, ..., title = NULL, first_pillar = NULL) |
| 173 |
# { |
| 174 |
# "!!!!DEBUG ctl_new_pillar_list.tbl(`v(width)`, `v(title)`)" |
| 175 |
# if (is.data.frame(x)) { |
| 176 |
# new_data_frame_pillar_list(x, controller, width, title = title, |
| 177 |
# first_pillar = first_pillar) |
| 178 |
# } |
| 179 |
# else if (is.matrix(x) && !inherits(x, c("Surv", "Surv2"))) { |
| 180 |
# new_matrix_pillar_list(x, controller, width, title = title, |
| 181 |
# first_pillar = first_pillar) |
| 182 |
# } |
| 183 |
# else if (is.array(x) && length(dim(x)) > 2) { |
| 184 |
# new_array_pillar_list(x, controller, width, title = title, |
| 185 |
# first_pillar = first_pillar) |
| 186 |
# } |
| 187 |
# else { |
| 188 |
# if (is.null(first_pillar)) { |
| 189 |
# first_pillar <- ctl_new_pillar(controller, x, width, |
| 190 |
# ..., title = prepare_title(title)) |
| 191 |
# } |
| 192 |
# new_single_pillar_list(first_pillar, width) |
| 193 |
# } |
| 194 |
# } |
| 195 |
|
| 196 |
## ----------------------------------------------------------------------------- |
| 197 |
ctl_new_pillar(tbl, tbl$a, width = 20) |
| 198 |
|
| 199 |
## ----show_source = TRUE------------------------------------------------------- |
| 200 |
# ctl_new_pillar.tbl <- function (controller, x, width, ..., title = NULL) |
| 201 |
# { |
| 202 |
# "!!!!DEBUG ctl_new_pillar.tbl(`v(width)`, `v(title)`)" |
| 203 |
# pillar(x, title, if (!is.null(width)) |
| 204 |
# max0(width)) |
| 205 |
# } |
| 206 |
|
| 207 |
## ----show_source = TRUE------------------------------------------------------- |
| 208 |
# pillar <- function (x, title = NULL, width = NULL, ...) |
| 209 |
# { |
| 210 |
# "!!!!DEBUG pillar(`v(class(x))`, `v(title)`, `v(width)`)" |
| 211 |
# pillar_from_shaft(new_pillar_title(title), new_pillar_type(x), |
| 212 |
# pillar_shaft(x, ...), width) |
| 213 |
# } |
| 214 |
|
| 215 |
## ----show_source = TRUE------------------------------------------------------- |
| 216 |
# new_pillar <- function (components, ..., width = NULL, class = NULL, extra = deprecated()) |
| 217 |
# { |
| 218 |
# "!!!!DEBUG new_pillar(`v(width)`, `v(class)`)" |
| 219 |
# if (is_present(extra)) { |
| 220 |
# deprecate_warn("1.7.0", "pillar::new_pillar(extra = )") |
| 221 |
# } |
| 222 |
# check_dots_empty() |
| 223 |
# if (length(components) > 0 && !is_named(components)) { |
| 224 |
# abort("All components must have names.") |
| 225 |
# } |
| 226 |
# structure(components, width = width, class = c(class, "pillar")) |
| 227 |
# } |
| 228 |
|
| 229 |
## ----show_source = TRUE------------------------------------------------------- |
| 230 |
# format.pillar <- function (x, width = NULL, ...) |
| 231 |
# { |
| 232 |
# if (is.null(width)) { |
| 233 |
# width <- get_width(x) |
| 234 |
# } |
| 235 |
# if (is.null(width)) { |
| 236 |
# width <- pillar_get_width(x) |
| 237 |
# } |
| 238 |
# as_glue(pillar_format_parts_2(x, width)$aligned) |
| 239 |
# } |
| 240 |
|