| 1 |
#' Constructor for column data |
| 2 |
#' |
| 3 |
#' @description |
| 4 |
#' The `new_pillar_shaft()` constructor creates objects of the `"pillar_shaft"` |
| 5 |
#' class. |
| 6 |
#' This is a virtual or abstract class, you must specify the `class` |
| 7 |
#' argument. |
| 8 |
#' By convention, this should be a string that starts with `"pillar_shaft_"`. |
| 9 |
#' See `vignette("extending", package = "tibble")` for usage examples. |
| 10 |
#' |
| 11 |
#' This method accepts a vector of arbitrary length and is expected to return an S3 object with the following properties: |
| 12 |
#' |
| 13 |
#' - It has an attribute `"width"` |
| 14 |
#' - It can have an attribute `"min_width"`, if missing, `"width"` is used |
| 15 |
#' - It must implement a method `format(x, width, ...)` that can be called with any value between `min_width` and `width` |
| 16 |
#' - This method must return an object that inherits from `character` and has attributes `"align"` (with supported values `"left"`, `"right"`, and `"center"`) and `"width"` |
| 17 |
#' |
| 18 |
#' The function [new_pillar_shaft()] returns such an object, and also correctly formats `NA` values. |
| 19 |
#' In many cases, the implementation of `pillar_shaft.your_class_name()` will format the data as a character vector (using color for emphasis) and simply call `new_pillar_shaft()`. |
| 20 |
#' See `pillar:::pillar_shaft.numeric` for a code that allows changing the display depending on the available width. |
| 21 |
#' |
| 22 |
#' @param x An object |
| 23 |
#' @param ... Additional attributes. |
| 24 |
#' @param width The maximum column width. |
| 25 |
#' @param min_width The minimum allowed column width, `width` if omitted. |
| 26 |
#' @param type_sum `r lifecycle::badge("experimental")` |
| 27 |
#' |
| 28 |
#' Override the type summary displayed at the top of the data. |
| 29 |
#' This argument, if given, takes precedence over the type summary provided by |
| 30 |
#' [type_sum()]. |
| 31 |
#' @param class The name of the subclass. |
| 32 |
#' @param subclass Deprecated, pass the `class` argument instead. |
| 33 |
#' @name new_pillar_shaft |
| 34 |
#' @export |
| 35 |
new_pillar_shaft <- function(x, ..., width = NULL, min_width = width, |
| 36 |
type_sum = NULL, class = NULL, subclass = NULL) { |
| 37 |
if (!is.null(subclass)) { |
| 38 |
deprecate_stop("1.4.0", "pillar::new_pillar_shaft(subclass = )", "new_pillar_shaft(class = )") |
| 39 |
class <- subclass |
| 40 |
} |
| 41 |
|
| 42 |
stopifnot(is.character(class)) |
| 43 |
stopifnot(length(class) > 0) |
| 44 |
stopifnot(is_bare_numeric(width, 1)) |
| 45 |
|
| 46 |
ret <- structure( |
| 47 |
x, |
| 48 |
..., |
| 49 |
type = if (!is.null(type_sum)) new_pillar_type_obj(type_sum), |
| 50 |
class = c(class, "pillar_shaft") |
| 51 |
) |
| 52 |
ret <- set_width(ret, width) |
| 53 |
ret <- set_min_width(ret, min_width) |
| 54 |
ret |
| 55 |
} |
| 56 |
|
| 57 |
#' Column data |
| 58 |
#' |
| 59 |
#' Internal class for formatting the data for a column. |
| 60 |
#' `pillar_shaft()` is a coercion method that must be implemented |
| 61 |
#' for your data type to display it in a tibble. |
| 62 |
#' |
| 63 |
#' @param x A vector to format |
| 64 |
#' @inheritParams rlang::args_dots_used |
| 65 |
#' @export |
| 66 |
#' @examples |
| 67 |
#' pillar_shaft(1:3) |
| 68 |
#' pillar_shaft(1.5:3.5) |
| 69 |
#' pillar_shaft(NA) |
| 70 |
#' pillar_shaft(c(1:3, NA)) |
| 71 |
pillar_shaft <- function(x, ...) { |
| 72 |
"!!!!DEBUG pillar_shaft(`v(class(x))`)" |
| 73 |
|
| 74 |
if (!missing(...)) { |
| 75 |
check_dots_used(action = warn) |
| 76 |
} |
| 77 |
|
| 78 |
UseMethod("pillar_shaft") |
| 79 |
} |
| 80 |
|
| 81 |
#' @export |
| 82 |
pillar_shaft.pillar_empty_col <- function(x, ...) { |
| 83 |
new_empty_shaft() |
| 84 |
} |
| 85 |
|
| 86 |
#' @param width Width for printing and formatting. |
| 87 |
#' @export |
| 88 |
#' @rdname pillar_shaft |
| 89 |
print.pillar_shaft <- function(x, width = NULL, ...) { |
| 90 |
#' @description |
| 91 |
#' This class comes with a default method for [print()] that calls [format()]. |
| 92 |
#' If `print()` is called without `width` argument, the natural width will be |
| 93 |
#' used when calling `format()`. |
| 94 |
#' Usually there's no need to implement this method for your subclass. |
| 95 |
if (is.null(width)) width <- get_width(x) |
| 96 |
print(format(x, width = width, ...)) |
| 97 |
} |
| 98 |
|
| 99 |
#' @export |
| 100 |
#' @rdname pillar_shaft |
| 101 |
format.pillar_shaft <- function(x, width, ...) { |
| 102 |
#' @description |
| 103 |
#' Your subclass must implement `format()`, the default implementation just |
| 104 |
#' raises an error. |
| 105 |
#' Your `format()` method can assume a valid value for the `width` argument. |
| 106 |
stop("Please implement a format() method for class ", class(x)[[1]], call. = FALSE) |
| 107 |
} |
| 108 |
|
| 109 |
# Methods ----------------------------------------------------------------- |
| 110 |
|
| 111 |
#' @export |
| 112 |
#' @rdname pillar_shaft |
| 113 |
pillar_shaft.logical <- function(x, ...) { |
| 114 |
out <- rep(NA, length(x)) |
| 115 |
out[x] <- "TRUE" |
| 116 |
out[!x] <- "FALSE" |
| 117 |
|
| 118 |
new_pillar_shaft_simple(out, width = 5, align = "left") |
| 119 |
} |
| 120 |
|
| 121 |
#' @export |
| 122 |
#' @rdname pillar_shaft |
| 123 |
#' @param sigfig |
| 124 |
#' Deprecated, use [num()] or [set_num_opts()] on the data instead. |
| 125 |
pillar_shaft.numeric <- function(x, ..., sigfig = NULL) { |
| 126 |
pillar_attr <- attr(x, "pillar", exact = TRUE) |
| 127 |
|
| 128 |
if (is.null(pillar_attr) && !is.null(attr(x, "class", exact = TRUE))) { |
| 129 |
ret <- format(x) |
| 130 |
return(new_pillar_shaft_simple(ret, width = get_max_extent(ret), align = "left")) |
| 131 |
} |
| 132 |
|
| 133 |
pillar_shaft_number_attr(unclass(x), pillar_attr, sigfig) |
| 134 |
} |
| 135 |
|
| 136 |
pillar_shaft_number_attr <- function(data, pillar_attr, sigfig = NULL) { |
| 137 |
scale <- pillar_attr$scale |
| 138 |
if (!is.null(scale)) { |
| 139 |
data <- data * scale |
| 140 |
} |
| 141 |
|
| 142 |
pillar_shaft_number( |
| 143 |
data, |
| 144 |
sigfig %||% pillar_attr$sigfig, |
| 145 |
pillar_attr$digits, |
| 146 |
pillar_attr$notation, |
| 147 |
pillar_attr$fixed_exponent, |
| 148 |
pillar_attr$extra_sigfig |
| 149 |
) |
| 150 |
} |
| 151 |
|
| 152 |
pillar_shaft_number <- function( |
| 153 |
x, |
| 154 |
sigfig = NULL, |
| 155 |
digits = NULL, |
| 156 |
notation = NULL, |
| 157 |
fixed_exponent = NULL, |
| 158 |
extra_sigfig = NULL |
| 159 |
) { |
| 160 |
if (!is.null(digits)) { |
| 161 |
if (!is.numeric(digits) || length(digits) != 1) { |
| 162 |
abort("`digits` must be a number.") |
| 163 |
} |
| 164 |
} |
| 165 |
if (is.null(sigfig)) { |
| 166 |
sigfig <- get_pillar_option_sigfig() |
| 167 |
} |
| 168 |
|
| 169 |
if (isTRUE(extra_sigfig)) { |
| 170 |
sigfig <- sigfig + compute_extra_sigfig(x) |
| 171 |
} |
| 172 |
|
| 173 |
if (is.null(notation) || notation == "fit") { |
| 174 |
dec <- split_decimal(x, sigfig = sigfig, digits = digits) |
| 175 |
sci <- split_decimal(x, sigfig = sigfig, digits = digits, sci_mod = 1, fixed_exponent = fixed_exponent) |
| 176 |
|
| 177 |
max_dec_width <- get_pillar_option_max_dec_width() |
| 178 |
dec_width <- get_width(dec) |
| 179 |
"!!!!!!DEBUG `v(dec_width)`" |
| 180 |
|
| 181 |
if (dec_width > max_dec_width) { |
| 182 |
dec <- NULL |
| 183 |
} |
| 184 |
} else if (notation == "dec") { |
| 185 |
dec <- split_decimal(x, sigfig = sigfig, digits = digits) |
| 186 |
sci <- NULL |
| 187 |
} else if (notation == "sci") { |
| 188 |
sci <- split_decimal( |
| 189 |
x, |
| 190 |
sigfig = sigfig, digits = digits, |
| 191 |
sci_mod = 1, |
| 192 |
fixed_exponent = fixed_exponent |
| 193 |
) |
| 194 |
dec <- NULL |
| 195 |
} else if (notation == "eng") { |
| 196 |
sci <- split_decimal( |
| 197 |
x, |
| 198 |
sigfig = sigfig, digits = digits, sci_mod = 3, |
| 199 |
fixed_exponent = fixed_exponent |
| 200 |
) |
| 201 |
dec <- NULL |
| 202 |
} else if (notation == "si") { |
| 203 |
sci <- split_decimal( |
| 204 |
x, |
| 205 |
sigfig = sigfig, digits = digits, sci_mod = 3, si = TRUE, |
| 206 |
fixed_exponent = fixed_exponent |
| 207 |
) |
| 208 |
dec <- NULL |
| 209 |
} else { |
| 210 |
abort(paste0('Internal error: `notation = "', notation, '".')) |
| 211 |
} |
| 212 |
|
| 213 |
ret <- list() |
| 214 |
ret$dec <- dec |
| 215 |
ret$sci <- sci |
| 216 |
|
| 217 |
if (!is.null(ret$sci$unit) && ret$sci$unit != 0) { |
| 218 |
type_sum <- I(paste0( |
| 219 |
# Turn off subtle styling for units |
| 220 |
sub(" ", "", style_subtle(" ")), |
| 221 |
"[", |
| 222 |
format_exp(ret$sci$unit, (notation == "si")), |
| 223 |
"]" |
| 224 |
)) |
| 225 |
} else { |
| 226 |
type_sum <- NULL |
| 227 |
} |
| 228 |
|
| 229 |
new_pillar_shaft( |
| 230 |
ret, |
| 231 |
width = get_width(ret$dec %||% ret$sci), |
| 232 |
min_width = min(get_min_widths(ret)), |
| 233 |
type_sum = type_sum, |
| 234 |
class = "pillar_shaft_decimal" |
| 235 |
) |
| 236 |
} |
| 237 |
|
| 238 |
#' @export |
| 239 |
pillar_shaft.integer64 <- function(x, ..., sigfig = NULL) { |
| 240 |
if (class(x)[[1]] != "integer64") { |
| 241 |
return(NextMethod()) |
| 242 |
} |
| 243 |
|
| 244 |
pillar_shaft_number(x, sigfig, digits = NULL, notation = NULL, fixed_exponent = NULL, extra_sigfig = NULL) |
| 245 |
} |
| 246 |
|
| 247 |
#' @export |
| 248 |
pillar_shaft.Surv <- function(x, ...) { |
| 249 |
new_pillar_shaft_simple(format(x), align = "right") |
| 250 |
} |
| 251 |
|
| 252 |
#' @export |
| 253 |
pillar_shaft.Surv2 <- function(x, ...) { |
| 254 |
new_pillar_shaft_simple(format(x), align = "right") |
| 255 |
} |
| 256 |
|
| 257 |
#' @export |
| 258 |
type_sum.Surv <- function(x) { |
| 259 |
"Surv" |
| 260 |
} |
| 261 |
|
| 262 |
#' @export |
| 263 |
type_sum.Surv2 <- function(x) { |
| 264 |
"Surv2" |
| 265 |
} |
| 266 |
|
| 267 |
#' @export |
| 268 |
#' @rdname pillar_shaft |
| 269 |
pillar_shaft.Date <- function(x, ...) { |
| 270 |
x <- format(x, format = "%Y-%m-%d") |
| 271 |
|
| 272 |
new_pillar_shaft_simple(x, width = 10, align = "left") |
| 273 |
} |
| 274 |
|
| 275 |
#' @export |
| 276 |
#' @rdname pillar_shaft |
| 277 |
pillar_shaft.POSIXt <- function(x, ...) { |
| 278 |
width <- 19L |
| 279 |
digits_secs <- getOption("digits.secs", 0L) |
| 280 |
if (digits_secs > 0) { |
| 281 |
width <- width + min(digits_secs, 6) + 1L |
| 282 |
} |
| 283 |
|
| 284 |
date <- format(x, format = "%Y-%m-%d") |
| 285 |
time <- format(x, format = "%H:%M:%OS") |
| 286 |
|
| 287 |
# R < 4.3 don't support is.finite() for POSIXlt |
| 288 |
time_styled <- paste0(" ", style_subtle(time)) |
| 289 |
if (inherits(x, "POSIXct")) { |
| 290 |
time_styled[!is.finite(x)] <- "" |
| 291 |
} |
| 292 |
|
| 293 |
datetime <- paste0(date, time_styled) |
| 294 |
datetime[is.na(x)] <- NA |
| 295 |
|
| 296 |
new_pillar_shaft_simple(datetime, width = width, align = "left") |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
#' @export |
| 301 |
#' @rdname pillar_shaft |
| 302 |
#' @param min_width |
| 303 |
#' Deprecated, use [char()] or [set_char_opts()] on the data instead. |
| 304 |
pillar_shaft.character <- function(x, ..., min_width = NULL) { |
| 305 |
pillar_attr <- attr(x, "pillar", exact = TRUE) |
| 306 |
|
| 307 |
min_chars <- min_width %||% pillar_attr$min_chars |
| 308 |
|
| 309 |
x <- utf8::utf8_encode(x) |
| 310 |
out <- x |
| 311 |
|
| 312 |
# Add subtle quotes if necessary |
| 313 |
needs_quotes <- which(is_ambiguous_string(x)) |
| 314 |
if (has_length(needs_quotes)) { |
| 315 |
quoted <- gsub('"', '\\"', x[needs_quotes], fixed = TRUE) |
| 316 |
out[needs_quotes] <- quoted |
| 317 |
valid <- paste0(style_subtle('"'), out[!is.na(x)], style_subtle('"')) |
| 318 |
out[!is.na(x)] <- valid |
| 319 |
na_indent <- 1 |
| 320 |
} else { |
| 321 |
na_indent <- 0 |
| 322 |
} |
| 323 |
|
| 324 |
# determine width based on width of characters in the vector |
| 325 |
if (is.null(min_chars)) { |
| 326 |
min_chars <- get_pillar_option_min_chars() |
| 327 |
} |
| 328 |
|
| 329 |
if (isS4(out)) { |
| 330 |
# Remove S4 bit: |
| 331 |
out <- unclass(out)[seq_along(out)] |
| 332 |
} |
| 333 |
|
| 334 |
pillar_shaft(as_glue(out), ..., min_width = min_chars, na_indent = na_indent, shorten = pillar_attr$shorten) |
| 335 |
} |
| 336 |
|
| 337 |
#' @export |
| 338 |
#' @inheritParams new_pillar_shaft_simple |
| 339 |
#' @rdname pillar_shaft |
| 340 |
pillar_shaft.glue <- function(x, ..., min_width = NULL, na_indent = 0L, shorten = NULL) { |
| 341 |
min_width <- max(min_width, 3L) |
| 342 |
width <- get_max_extent(x) |
| 343 |
na <- pillar_na(use_brackets_if_no_color = TRUE) |
| 344 |
force(na_indent) |
| 345 |
|
| 346 |
new_pillar_shaft_simple( |
| 347 |
x, |
| 348 |
width = width, align = "left", min_width = min(width, min_width), |
| 349 |
na = na, |
| 350 |
na_indent = if (width > get_extent(na)) na_indent else 0, |
| 351 |
shorten = shorten |
| 352 |
) |
| 353 |
} |
| 354 |
|
| 355 |
#' @export |
| 356 |
#' @rdname pillar_shaft |
| 357 |
pillar_shaft.list <- function(x, ...) { |
| 358 |
summary <- map(x, obj_sum) |
| 359 |
|
| 360 |
formatted <- paste0("<", unlist(summary), ">") |
| 361 |
|
| 362 |
short <- map(summary, attr, "short") |
| 363 |
short_idx <- !map_lgl(short, is.null) |
| 364 |
short_formatted <- formatted |
| 365 |
short_formatted[short_idx] <- paste0("<", unlist(short[short_idx]), ">") |
| 366 |
|
| 367 |
width <- get_max_extent(formatted) |
| 368 |
min_width <- get_max_extent(short_formatted) |
| 369 |
|
| 370 |
new_pillar_shaft_simple( |
| 371 |
style_list(formatted), |
| 372 |
width = width, |
| 373 |
align = "left", |
| 374 |
min_width = min_width, |
| 375 |
short_formatted = style_list(short_formatted) |
| 376 |
) |
| 377 |
} |
| 378 |
|
| 379 |
#' @export |
| 380 |
#' @rdname pillar_shaft |
| 381 |
pillar_shaft.factor <- function(x, ...) { |
| 382 |
pillar_shaft(as.character(x), ...) |
| 383 |
} |
| 384 |
|
| 385 |
#' @export |
| 386 |
#' @rdname pillar_shaft |
| 387 |
pillar_shaft.AsIs <- function(x, ...) { |
| 388 |
pillar_shaft(remove_as_is_class(x)) |
| 389 |
} |
| 390 |
|
| 391 |
#' @export |
| 392 |
#' @rdname pillar_shaft |
| 393 |
pillar_shaft.default <- function(x, ...) { |
| 394 |
#' @details |
| 395 |
#' The default method will currently format via [format()], |
| 396 |
#' but you should not rely on this behavior. |
| 397 |
pillar_shaft(as_glue(format(x)), ...) |
| 398 |
} |