-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathdim.R
41 lines (37 loc) · 1 KB
/
dim.R
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
#' Format dimensions
#'
#' Multi-dimensional objects are formatted as `a x b x ...`, for vectors the
#' length is returned.
#'
#' @param x The object to format the dimensions for
#'
#' @export
#' @examples
#' dim_desc(1:10)
#' dim_desc(Titanic)
dim_desc <- function(x) {
dim <- dim2(x)
format_dim <- big_mark(dim)
format_dim[is.na(dim)] <- "??"
paste0(format_dim, collapse = spaces_around(mult_sign()))
}
# https://github.com/r-lib/vctrs/pull/467
dim2 <- function(x) {
dim(x) %||% vec_size(x)
}
mult_sign <- function() {
symbol$times
}
spaces_around <- function(x) {
paste0(" ", x, " ")
}
# function for the thousand separator,
# returns "," unless it's used for the decimal point, in which case returns "."
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", preserve.width = "individual")
ret[is.na(x)] <- "??"
ret
}