forked from tidyverse/dplyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.R
176 lines (144 loc) · 3.8 KB
/
error.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
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
check_pkg <- function(name, reason, install = TRUE) {
if (is_installed(name)) {
return(invisible(TRUE))
}
glubort(NULL, "The {name} package is required to {reason}.",
if (install) '\nPlease install it with `install.packages("{name}")`' else ""
)
}
# ngettext() does extra work, this function is a simpler version
ntext <- function(n, msg1, msg2) {
if (n == 1) msg1 else msg2
}
bad <- function(..., .envir = parent.frame()) {
glubort(NULL, ..., .envir = parent.frame())
}
bad_args <- function(args, ..., .envir = parent.frame()) {
glubort(fmt_args(args), ..., .envir = .envir)
}
bad_pos_args <- function(pos_args, ..., .envir = parent.frame()) {
glubort(fmt_pos_args(pos_args), ..., .envir = .envir)
}
bad_calls <- function(calls, ..., .envir = parent.frame()) {
glubort(fmt_calls(calls), ..., .envir = .envir)
}
glubort <- function(header, ..., .envir = parent.frame(), .abort = abort) {
text <- glue(..., .envir = .envir)
if (!is_null(header)) text <- paste0(header, " ", text)
.abort(text)
}
fmt_args <- function(x) {
x <- parse_args(x)
fmt_obj(x)
}
fmt_pos_args <- function(x) {
args <- ntext(length(x), "Argument", "Arguments")
glue("{args} {fmt_comma(x)}")
}
fmt_calls <- function(...) {
x <- parse_named_call(...)
fmt_obj(x)
}
fmt_named_calls <- function(...) {
x <- parse_named_call(...)
fmt_named(x)
}
fmt_wrong_eq_ops <- function(...) {
x <- parse_named_call(...)
fmt_items(
paste0("Did you mean ", fmt_obj1(paste0(names2(x), " == ", x)), "?"),
bullet = "*"
)
}
fmt_cols <- function(x) {
cols <- ntext(length(x), "Column", "Columns")
glue("{cols} {fmt_obj(x)}")
}
fmt_measures <- function(x) {
measures <- ntext(length(x), "Measure", "Measures")
glue("{measures} {fmt_obj(x)}")
}
fmt_named <- function(x) {
fmt_comma(paste0(fmt_obj1(names2(x)), " = ", x))
}
fmt_obj <- function(x) {
fmt_comma(fmt_obj1(x))
}
fmt_obj1 <- function(x) {
paste0("`", x, "`")
}
fmt_classes <- function(x) {
paste(class(x), collapse = "/")
}
fmt_dims <- function(x) {
paste0("[", paste0(x, collapse = " x "), "]")
}
fmt_comma <- function(..., .max = 6) {
x <- paste0(...)
if (length(x) > .max) {
length(x) <- .max
x[[.max]] <- "..."
}
commas(x)
}
fmt_items <- function(x, bullet = "-", .max = 6) {
if (length(x) > .max) {
more <- glue("({length(x) - (.max - 1)} more)")
length(x) <- .max
x[.max] <- more
}
paste0(glue("{bullet} {x}"), collapse = "\n")
}
parse_args <- function(x) {
# convert single formula to list of length 1
x <- unlist(list(x), recursive = FALSE)
is_fml <- map_lgl(x, is_formula)
x[is_fml] <- map_chr(map(x[is_fml], "[[", 2), as_string)
unlist(x)
}
parse_named_call <- function(x) {
map_chr(x, quo_text)
}
# From rlang
friendly_type_of <- function(x) {
if (is.object(x)) {
sprintf("a `%s` object", fmt_classes(x))
} else {
as_friendly_type(typeof(x))
}
}
as_friendly_type <- function(type) {
switch(type,
logical = "a logical vector",
integer = "an integer vector",
numeric = ,
double = "a double vector",
complex = "a complex vector",
character = "a character vector",
raw = "a raw vector",
string = "a string",
list = "a list",
NULL = "NULL",
environment = "an environment",
externalptr = "a pointer",
weakref = "a weak reference",
S4 = "an S4 object",
name = ,
symbol = "a symbol",
language = "a call",
pairlist = "a pairlist node",
expression = "an expression vector",
quosure = "a quosure",
formula = "a formula",
char = "an internal string",
promise = "an internal promise",
... = "an internal dots object",
any = "an internal `any` object",
bytecode = "an internal bytecode object",
primitive = ,
builtin = ,
special = "a primitive function",
closure = "a function",
type
)
}