| 1 |
#' Format dimensions |
| 2 |
#' |
| 3 |
#' Multi-dimensional objects are formatted as `a x b x ...`, for vectors the |
| 4 |
#' length is returned. |
| 5 |
#' |
| 6 |
#' @param x The object to format the dimensions for |
| 7 |
#' |
| 8 |
#' @export |
| 9 |
#' @examples |
| 10 |
#' dim_desc(1:10) |
| 11 |
#' dim_desc(Titanic) |
| 12 |
dim_desc <- function(x) { |
| 13 |
dim <- dim2(x) |
| 14 |
format_dim <- big_mark(dim) |
| 15 |
format_dim[is.na(dim)] <- "??" |
| 16 |
paste0(format_dim, collapse = spaces_around(mult_sign())) |
| 17 |
} |
| 18 |
|
| 19 |
# https://github.com/r-lib/vctrs/pull/467 |
| 20 |
dim2 <- function(x) { |
| 21 |
dim(x) %||% vec_size(x) |
| 22 |
} |
| 23 |
|
| 24 |
mult_sign <- function() { |
| 25 |
symbol$times |
| 26 |
} |
| 27 |
|
| 28 |
spaces_around <- function(x) { |
| 29 |
paste0(" ", x, " ") |
| 30 |
} |
| 31 |
|
| 32 |
# function for the thousand separator, |
| 33 |
# returns "," unless it's used for the decimal point, in which case returns "." |
| 34 |
big_mark <- function(x) { |
| 35 |
# The thousand separator, |
| 36 |
# "," unless it's used for the decimal point, in which case "." |
| 37 |
mark <- if (identical(getOption("OutDec"), ",")) "." else "," |
| 38 |
ret <- formatC(x, big.mark = mark, format = "d", preserve.width = "individual") |
| 39 |
ret[is.na(x)] <- "??" |
| 40 |
ret |
| 41 |
} |