1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
|
#' Printing tibbles
#'
#' @description
#' One of the main features of the `tbl_df` class is the printing:
#'
#' * Tibbles only print as many rows and columns as fit on one screen,
#' supplemented by a summary of the remaining rows and columns.
#' * Tibble reveals the type of each column, which keeps the user informed about
#' whether a variable is, e.g., `<chr>` or `<fct>` (character versus factor).
#' See `vignette("types")` for an overview of common
#' type abbreviations.
#'
#' Printing can be tweaked for a one-off call by calling `print()` explicitly
#' and setting arguments like `n` and `width`. More persistent control is
#' available by setting the options described in [pillar::pillar_options].
#' See also `vignette("digits")` for a comparison to base options,
#' and `vignette("numbers")` that showcases [num()] and [char()]
#' for creating columns with custom formatting options.
#'
#' As of tibble 3.1.0, printing is handled entirely by the \pkg{pillar} package.
#' If you implement a package that extends tibble,
#' the printed output can be customized in various ways.
#' See `vignette("extending", package = "pillar")` for details,
#' and [pillar::pillar_options] for options that control the display in the console.
#'
# Copied from pillar::format.tbl() to avoid roxygen2 warning
#' @param x Object to format or print.
#' @param ... Passed on to [tbl_format_setup()].
#' @param n Number of rows to show. If `NULL`, the default, will print all rows
#' if less than the `print_max` [option][pillar::pillar_options].
#' Otherwise, will print as many rows as specified by the
#' `print_min` [option][pillar::pillar_options].
#' @param width Width of text output to generate. This defaults to `NULL`, which
#' means use the `width` [option][pillar::pillar_options].
#' @param max_extra_cols Number of extra columns to print abbreviated information for,
#' if the width is too small for the entire tibble. If `NULL`,
#' the `max_extra_cols` [option][pillar::pillar_options] is used.
#' The previously defined `n_extra` argument is soft-deprecated.
#' @param max_footer_lines Maximum number of footer lines. If `NULL`,
#' the `max_footer_lines` [option][pillar::pillar_options] is used.
#'
#' @examples
#' print(as_tibble(mtcars))
#' print(as_tibble(mtcars), n = 1)
#' print(as_tibble(mtcars), n = 3)
#'
#' print(as_tibble(trees), n = 100)
#'
#' print(mtcars, width = 10)
#'
#' mtcars2 <- as_tibble(cbind(mtcars, mtcars), .name_repair = "unique")
#' print(mtcars2, n = 25, max_extra_cols = 3)
#'
#' @examplesIf requireNamespace("nycflights13", quietly = TRUE)
#' print(nycflights13::flights, max_footer_lines = 1)
#' print(nycflights13::flights, width = Inf)
#'
#' @name formatting
#' @aliases print.tbl format.tbl
NULL
# Only for documentation, doesn't do anything
#' @rdname formatting
print.tbl_df <- function(x, width = NULL, ..., n = NULL, max_extra_cols = NULL,
max_footer_lines = NULL) {
NextMethod()
}
# Only for documentation, doesn't do anything
#' @rdname formatting
format.tbl_df <- function(x, width = NULL, ..., n = NULL, max_extra_cols = NULL,
max_footer_lines = NULL) {
NextMethod()
}
#' Legacy printing
#'
#' @description
#' `r lifecycle::badge("deprecated")`
#' As of tibble 3.1.0, printing is handled entirely by the \pkg{pillar} package.
#' Do not use this function.
#' If you implement a package that extend tibble,
#' the printed output can be customized in various ways.
#' See `vignette("extending", package = "pillar")` for details.
#'
#' @param x Object to format or print.
#' @param n Number of rows to show. If `NULL`, the default, will print all rows
#' if less than option `tibble.print_max`. Otherwise, will print
#' `tibble.print_min` rows.
#' @param width Width of text output to generate. This defaults to `NULL`, which
#' means use `getOption("tibble.width")` or (if also `NULL`)
#' `getOption("width")`; the latter displays only the columns that fit on one
#' screen. You can also set `options(tibble.width = Inf)` to override this
#' default and always print all columns, this may be slow for very wide tibbles.
#' @param n_extra Number of extra columns to print abbreviated information for,
#' if the width is too small for the entire tibble. If `NULL`, the default,
#' will print information about at most `tibble.max_extra_cols` extra columns.
#'
#' @return An object with a `print()` method that will print the input
#' similarly to a tibble.
#' The internal data format is an implementation detail, do not rely on it.
#' @export
#' @keywords internal
trunc_mat <- function(x, n = NULL, width = NULL, n_extra = NULL) {
deprecate_soft("3.1.0", "tibble::trunc_mat()",
details = "Printing has moved to the pillar package."
)
rows <- nrow(x)
if (is.null(n) || n < 0) {
if (is.na(rows) || rows > tibble_opt("print_max")) {
n <- tibble_opt("print_min")
} else {
n <- rows
}
}
n_extra <- n_extra %||% tibble_opt("max_extra_cols")
if (is.na(rows)) {
df <- as.data.frame(head(x, n + 1))
if (nrow(df) <= n) {
rows <- nrow(df)
} else {
df <- df[seq_len(n), , drop = FALSE]
}
} else {
df <- as.data.frame(head(x, n))
}
shrunk <- shrink_mat(df, rows, n, star = has_rownames(x))
trunc_info <- list(
width = width, rows_total = rows, rows_min = nrow(df),
n_extra = n_extra, summary = tbl_sum(x)
)
structure(
c(shrunk, trunc_info),
class = c(paste0("trunc_mat_", class(x)), "trunc_mat")
)
}
shrink_mat <- function(df, rows, n, star) {
df <- remove_rownames(df)
if (is.na(rows)) {
needs_dots <- (nrow(df) >= n)
} else {
needs_dots <- (rows > n)
}
if (needs_dots) {
rows_missing <- rows - n
} else {
rows_missing <- 0L
}
mcf <- pillar::colonnade(
df,
has_row_id = if (star) "*" else TRUE
)
list(.do_not_rely_on_this_mcf = mcf, .do_not_rely_on_this_rows_missing = rows_missing)
}
#' @importFrom pillar style_subtle
#' @export
format.trunc_mat <- function(x, width = NULL, ...) {
if (is.null(width)) {
width <- x$width
}
width <- tibble_width(width)
named_header <- format_header(x)
if (all(names2(named_header) == "")) {
header <- named_header
} else {
header <- paste0(
justify(
paste0(names2(named_header), ":"),
right = FALSE, space = NBSP
),
# We add a space after the NBSP inserted by justify()
# so that wrapping occurs at the right location for very narrow outputs
" ",
named_header
)
}
comment <- format_comment(header, width = width)
squeezed <- pillar::squeeze(x$.do_not_rely_on_this_mcf, width = width)
mcf <- format_body(squeezed)
# Splitting lines is important, otherwise subtle style may be lost
# if column names contain spaces.
footer <- pre_dots(format_footer(x, squeezed))
footer_comment <- split_lines(format_comment(footer, width = width))
c(style_subtle(comment), mcf, style_subtle(footer_comment))
}
# Needs to be defined in package code: r-lib/pkgload#85
print_with_mocked_format_body <- function(x, ...) {
scoped_lifecycle_silence()
mockr::with_mock(
format_body = function(x, ...) {
paste0("<body created by pillar>")
},
{
print(x, ...)
}
)
}
#' @export
print.trunc_mat <- function(x, ...) {
cli::cat_line(format(x, ...))
invisible(x)
}
format_header <- function(x) {
x$summary
}
format_body <- function(x) {
format(x)
}
format_footer <- function(x, squeezed_colonnade) {
extra_rows <- format_footer_rows(x)
extra_cols <- format_footer_cols(x, pillar::extra_cols(squeezed_colonnade, n = x$n_extra))
extra <- c(extra_rows, extra_cols)
if (length(extra) >= 1) {
extra[[1]] <- paste0("with ", extra[[1]])
extra[-1] <- map_chr(extra[-1], function(ex) paste0("and ", ex))
collapse(extra)
} else {
character()
}
}
format_footer_rows <- function(x) {
if (length(x$.do_not_rely_on_this_mcf) != 0) {
if (is.na(x$.do_not_rely_on_this_rows_missing)) {
"more rows"
} else if (x$.do_not_rely_on_this_rows_missing > 0) {
paste0(big_mark(x$.do_not_rely_on_this_rows_missing), pluralise_n(" more row(s)", x$.do_not_rely_on_this_rows_missing))
}
} else if (is.na(x$rows_total) && x$rows_min > 0) {
paste0("at least ", big_mark(x$rows_min), pluralise_n(" row(s) total", x$rows_min))
}
}
format_footer_cols <- function(x, extra_cols) {
if (length(extra_cols) == 0) return(NULL)
vars <- format_extra_vars(extra_cols)
paste0(
big_mark(length(extra_cols)), " ",
if (!identical(x$rows_total, 0L) && x$rows_min > 0) "more ",
pluralise("variable(s)", extra_cols), vars
)
}
format_extra_vars <- function(extra_cols) {
# Also covers empty extra_cols vector!
if (is.na(extra_cols[1])) return("")
if (anyNA(extra_cols)) {
extra_cols <- c(extra_cols[!is.na(extra_cols)], cli::symbol$ellipsis)
}
paste0(": ", collapse(extra_cols))
}
format_comment <- function(x, width) {
if (length(x) == 0L) return(character())
map_chr(x, wrap, prefix = "# ", width = min(width, getOption("width")))
}
pre_dots <- function(x) {
if (length(x) > 0) {
paste0(cli::symbol$ellipsis, " ", x)
} else {
character()
}
}
justify <- function(x, right = TRUE, space = " ") {
if (length(x) == 0L) return(character())
width <- nchar_width(x)
max_width <- max(width)
spaces_template <- paste(rep(space, max_width), collapse = "")
spaces <- map_chr(max_width - width, substr, x = spaces_template, start = 1L)
if (right) {
paste0(spaces, x)
} else {
paste0(x, spaces)
}
}
split_lines <- function(x) {
# Avoid .ptype argument to vec_c()
if (is_empty(x)) return(character())
unlist(strsplit(x, "\n", fixed = TRUE))
}
#' knit_print method for trunc mat
#' @keywords internal
#' @export
knit_print.trunc_mat <- function(x, options) {
header <- format_header(x)
if (length(header) > 0L) {
header[names2(header) != ""] <- paste0(names2(header), ": ", header)
summary <- header
} else {
summary <- character()
}
squeezed <- pillar::squeeze(x$.do_not_rely_on_this_mcf, x$width)
kable <- format_knitr_body(squeezed)
extra <- format_footer(x, squeezed)
if (length(extra) > 0) {
extra <- wrap("(", collapse(extra), ")", width = x$width)
} else {
extra <- "\n"
}
res <- paste(c("", "", summary, "", kable, "", extra), collapse = "\n")
knitr::asis_output(fansi::strip_sgr(res), cacheable = TRUE)
}
format_knitr_body <- function(x) {
knitr::knit_print(x)
}
big_mark <- function(x, ...) {
# The thousand separator,
# "," unless it's used for the decimal point, in which case "."
mark <- if (identical(getOption("OutDec"), ",")) "." else ","
ret <- formatC(x, big.mark = mark, format = "d", ...)
ret[is.na(x)] <- "??"
ret
}
mult_sign <- function() {
"x"
}
spaces_around <- function(x) {
paste0(" ", x, " ")
}
format_n <- function(x) collapse(quote_n(x))
quote_n <- function(x) UseMethod("quote_n")
#' @export
quote_n.default <- function(x) as.character(x)
#' @export
quote_n.character <- function(x) tick(x)
collapse <- function(x) paste(x, collapse = ", ")
# wrap --------------------------------------------------------------------
NBSP <- "\U00A0"
wrap <- function(..., indent = 0, prefix = "", width) {
x <- paste0(..., collapse = "")
wrapped <- strwrap2(x, width - nchar_width(prefix), indent)
wrapped <- paste0(prefix, wrapped)
wrapped <- gsub(NBSP, " ", wrapped)
paste0(wrapped, collapse = "\n")
}
strwrap2 <- function(x, width, indent) {
fansi::strwrap_ctl(x, width = max(width, 0), indent = indent, exdent = indent + 2)
}
|