forked from tidyverse/dplyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils-format.r
64 lines (55 loc) · 1.48 KB
/
utils-format.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#' Describing dimensions
#'
#' Prints the dimensions of an array-like object in a user-friendly manner,
#' substituting \code{NA} with ?? (for SQL queries).
#'
#' @param x Object to show dimensions for.
#' @export
#' @keywords internal
#' @examples
#' dim_desc(mtcars)
dim_desc <- function(x) {
d <- dim(x)
d2 <- big_mark(d)
d2[is.na(d)] <- "??"
paste0("[", paste0(d2, collapse = " x "), "]")
}
wrap <- function(..., indent = 0) {
x <- paste0(..., collapse = "")
wrapped <- strwrap(x, indent = indent, exdent = indent + 2,
width = getOption("width"))
paste0(wrapped, collapse = "\n")
}
ruler <- function(width = getOption("width")) {
x <- seq_len(width)
y <- case_when(
x %% 10 == 0 ~ as.character((x %/% 10) %% 10),
x %% 5 == 0 ~ "+",
TRUE ~ "-"
)
cat(y, "\n", sep = "")
cat(x %% 10, "\n", sep = "")
}
rule <- function(pad = "-", gap = 2L) {
paste0(rep(pad, getOption("width") - gap), collapse = "")
}
named_rule <- function(..., pad = "-") {
if (nargs() == 0) {
title <- ""
} else {
title <- paste0(...)
}
paste0(title, " ", rule(pad = pad, gap = nchar(title) - 1))
}
#' @export
print.BoolResult <- function(x, ...) {
cat(x)
if (!x) cat(": ", attr(x, "comment"), sep = "")
cat("\n")
}
# function for the thousand separator,
# returns "," unless it's used for the decimal point, in which case returns "."
big_mark <- function(x, ...) {
mark <- if (identical(getOption("OutDec"), ",")) "." else ","
formatC(x, big.mark = mark, ...)
}