-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathassert_inputs.R
280 lines (223 loc) · 7.79 KB
/
assert_inputs.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#' Assert that an input is a vector/list with desired properties
#'
#' @param x A vector/list. The input to be analysed.
#' @param class A string. The class that this input should inherit from.
#' @param len An integer. The length of the vector. If `NULL`, length is not
#' checked.
#' @param null_ok A logical. Whether the input could also be `NULL`.
#' @param var_name A string. The name of the variable in the informative
#' message. If `NULL`, the name is guessed from the function call.
#' @param subset_of A character vector. If `x` is a character vector, specifies
#' which values it can take. If `NULL`, values are not checked.
#' @param named A logical. Whether the input should be a named vector/list. If
#' `FALSE` this check is not enforced (i.e. the input object can be named even
#' if named is `FALSE`).
#' @param n_call A negative integer. The number of frames to go back to find the
#' call to associate the error generated by the function with. Defaults to the
#' parent frame (-1). This argument is only relevant to other assertion
#' functions that build on top of this one.
#'
#' @return `TRUE` if the check is successful. Throws an error describing the
#' issue with the input otherwise.
#'
#' @family input assertion
#'
#' @keywords internal
assert_vector <- function(x,
class,
len = NULL,
null_ok = FALSE,
var_name = NULL,
subset_of = NULL,
named = FALSE,
n_call = -1L) {
# basic input checking
if (!(is.character(class) && length(class) == 1)) {
error_bad_class_argument()
}
if (!is.null(len) && !(is.integer(len) && length(len) == 1)) {
error_bad_len_argument()
}
if (!(is.logical(null_ok) && length(null_ok) == 1)) {
error_bad_null_ok_argument()
}
if (!is.null(var_name) && !(is.character(var_name) && length(var_name) == 1))
error_bad_var_name_argument()
if (!is.null(subset_of) && !is.character(subset_of)) {
error_bad_subset_of_argument()
}
if (!(is.logical(named) && length(named) == 1)) {
error_bad_named_argument()
}
if (!(is.integer(n_call) && length(n_call) == 1 && n_call < 0)) {
error_bad_n_call_argument()
}
# construct input_name either from 'var_name' or from function call
current_call <- sys.call(0)
input_name <- as.character(current_call[[2]])
if (!is.null(var_name)) input_name <- var_name
# objects to create the gtfsio_error
error_call <- sys.call(n_call)
input_error_class <- paste0("bad_", input_name, "_argument")
input_name <- paste0("'", input_name, "'")
vector_name <- ifelse(
class == "list",
"a list.",
paste0("a(n) ", class, " vector.")
)
# check against desired properties
# the complicated logical condition below checks for the possibility of 'x'
# being NULL if null_ok is TRUE.
# also, it always consider NAs not ok, even when looking at logical vectors
if ((!inherits(x, class) && null_ok && !is.null(x))
|| (!inherits(x, class) && !null_ok)
|| (is.logical(x) && any(is.na(x)))) {
error_x_wrong_class(input_name, vector_name, input_error_class, error_call)
}
if (!is.null(len) && len != length(x)) {
error_x_wrong_length(input_name, len, input_error_class, error_call)
}
if (!is.null(subset_of) && (is.character(x) && any(! x %in% subset_of))) {
error_x_wrong_value(input_name, subset_of, input_error_class, error_call)
}
vector_name2 <- ifelse(class == "list", "list.", paste0(class, " vector."))
if (named && is.null(names(x)) && !is.null(x)) {
error_x_not_named(input_name, vector_name2, input_error_class, error_call)
}
non_empty_names <- names(x)[! names(x) %chin% ""]
if (named && (!is.null(names(x)) && length(non_empty_names) != length(x))) {
error_x_not_fully_named(input_name, input_error_class, error_call)
}
invisible(TRUE)
}
#' @rdname assert_vector
#' @family input assertion
#' @keywords internal
assert_list <- function(x, len = NULL, null_ok = FALSE, named = FALSE) {
# input checks are all conducted inside assert_vector()
call <- sys.call(0)
var_name <- as.character(call[[2]])
assert_vector(
x,
"list",
len = len,
null_ok = null_ok,
var_name = var_name,
named = named,
n_call = -2L
)
}
#' @rdname assert_vector
#' @family input assertion
#' @keywords internal
assert_class <- function(x, class, n_call = -1) {
assert_vector(class, "character")
# construct input_name from function call
current_call <- sys.call(0)
input_name <- as.character(current_call[[2]])
# objects to create the gtfsio_error
error_call <- sys.call(n_call)
input_error_class <- paste0("bad_", input_name, "_argument")
input_name <- paste0("'", input_name, "'")
# check against desired properties
if (!(all(inherits(x, class, which = TRUE)))) {
error_x_wrong_inheritance(input_name, class, input_error_class, error_call)
}
invisible(TRUE)
}
# errors ------------------------------------------------------------------
#' @include gtfsio_error.R
error_bad_class_argument <- parent_function_error(
"'class' must be a string.",
subclass = "bad_class_argument"
)
error_bad_len_argument <- parent_function_error(
"'length' must be an integer vector with length 1.",
subclass = "bad_len_argument"
)
error_bad_null_ok_argument <- parent_function_error(
"'null_ok' must be a logical vector with length 1.",
subclass = "bad_null_ok_argument"
)
error_bad_var_name_argument <- parent_function_error(
"'var_name' must be a string.",
subclass = "bad_var_name_argument"
)
error_bad_subset_of_argument <- parent_function_error(
"'subset_of' must be a character vector.",
subclass = "bad_subset_of_argument"
)
error_bad_named_argument <- parent_function_error(
"'named' must be a logical vector with length 1.",
subclass = "bad_named_argument"
)
error_bad_n_call_argument <- parent_function_error(
"'n_call' must be a negative integer.",
subclass = "bad_n_call_argument"
)
error_x_wrong_class <- function(input_name,
vector_name,
input_error_class,
error_call) {
gtfsio_error(
paste0(input_name, " must be ", vector_name),
input_error_class,
error_call
)
}
error_x_wrong_length <- function(input_name,
len,
input_error_class,
error_call) {
gtfsio_error(
paste0(input_name, " must have length ", len, "."),
input_error_class,
error_call
)
}
error_x_wrong_value <- function(input_name,
subset_of,
input_error_class,
error_call) {
gtfsio_error(
paste0(
input_name,
" must be a subset of [",
paste(paste0("'", subset_of, "'"), collapse = ", "),
"]."
),
input_error_class,
error_call
)
}
error_x_not_named <- function(input_name,
vector_name2,
input_error_class,
error_call) {
gtfsio_error(
paste0(input_name, " must be a named ", vector_name2),
input_error_class,
error_call
)
}
error_x_not_fully_named <- function(input_name, input_error_class, error_call) {
gtfsio_error(
paste0("Every element in ", input_name, " must be named."),
input_error_class,
error_call
)
}
error_x_wrong_inheritance <- function(input_name,
class,
input_error_class,
error_call) {
gtfsio_error(
paste0(
input_name, " must inherit from the ",
paste0("'", class, "'", collapse = ", "),
" class."
),
input_error_class,
error_call
)
}