Debian Codegraph @

R/import-standalone-types-check.R

raw
1 # Standalone file: do not edit by hand
2 # Source: https://github.com/r-lib/rlang/blob/HEAD/R/standalone-types-check.R
3 # Generated by: usethis::use_standalone("r-lib/rlang", "types-check")
4 # ----------------------------------------------------------------------
5 #
6 # ---
7 # repo: r-lib/rlang
8 # file: standalone-types-check.R
9 # last-updated: 2023-03-13
10 # license: https://unlicense.org
11 # dependencies: standalone-obj-type.R
12 # imports: rlang (>= 1.1.0)
13 # ---
14 #
15 # ## Changelog
16 #
17 # 2024-08-15:
18 # - `check_character()` gains an `allow_na` argument (@martaalcalde, #1724)
19 #
20 # 2023-03-13:
21 # - Improved error messages of number checkers (@teunbrand)
22 # - Added `allow_infinite` argument to `check_number_whole()` (@mgirlich).
23 # - Added `check_data_frame()` (@mgirlich).
24 #
25 # 2023-03-07:
26 # - Added dependency on rlang (>= 1.1.0).
27 #
28 # 2023-02-15:
29 # - Added `check_logical()`.
30 #
31 # - `check_bool()`, `check_number_whole()`, and
32 # `check_number_decimal()` are now implemented in C.
33 #
34 # - For efficiency, `check_number_whole()` and
35 # `check_number_decimal()` now take a `NULL` default for `min` and
36 # `max`. This makes it possible to bypass unnecessary type-checking
37 # and comparisons in the default case of no bounds checks.
38 #
39 # 2022-10-07:
40 # - `check_number_whole()` and `_decimal()` no longer treat
41 # non-numeric types such as factors or dates as numbers. Numeric
42 # types are detected with `is.numeric()`.
43 #
44 # 2022-10-04:
45 # - Added `check_name()` that forbids the empty string.
46 # `check_string()` allows the empty string by default.
47 #
48 # 2022-09-28:
49 # - Removed `what` arguments.
50 # - Added `allow_na` and `allow_null` arguments.
51 # - Added `allow_decimal` and `allow_infinite` arguments.
52 # - Improved errors with absent arguments.
53 #
54 #
55 # 2022-09-16:
56 # - Unprefixed usage of rlang functions with `rlang::` to
57 # avoid onLoad issues when called from rlang (#1482).
58 #
59 # 2022-08-11:
60 # - Added changelog.
61 #
62 # nocov start
63
64 # Scalars -----------------------------------------------------------------
65
66 .standalone_types_check_dot_call <- .Call
67
68 check_bool <- function(x,
69 ...,
70 allow_na = FALSE,
71 allow_null = FALSE,
72 arg = caller_arg(x),
73 call = caller_env()) {
74 if (!missing(x) && .standalone_types_check_dot_call(ffi_standalone_is_bool_1.0.7, x, allow_na, allow_null)) {
75 return(invisible(NULL))
76 }
77
78 stop_input_type(
79 x,
80 c("`TRUE`", "`FALSE`"),
81 ...,
82 allow_na = allow_na,
83 allow_null = allow_null,
84 arg = arg,
85 call = call
86 )
87 }
88
89 check_string <- function(x,
90 ...,
91 allow_empty = TRUE,
92 allow_na = FALSE,
93 allow_null = FALSE,
94 arg = caller_arg(x),
95 call = caller_env()) {
96 if (!missing(x)) {
97 is_string <- .rlang_check_is_string(
98 x,
99 allow_empty = allow_empty,
100 allow_na = allow_na,
101 allow_null = allow_null
102 )
103 if (is_string) {
104 return(invisible(NULL))
105 }
106 }
107
108 stop_input_type(
109 x,
110 "a single string",
111 ...,
112 allow_na = allow_na,
113 allow_null = allow_null,
114 arg = arg,
115 call = call
116 )
117 }
118
119 .rlang_check_is_string <- function(x,
120 allow_empty,
121 allow_na,
122 allow_null) {
123 if (is_string(x)) {
124 if (allow_empty || !is_string(x, "")) {
125 return(TRUE)
126 }
127 }
128
129 if (allow_null && is_null(x)) {
130 return(TRUE)
131 }
132
133 if (allow_na && (identical(x, NA) || identical(x, na_chr))) {
134 return(TRUE)
135 }
136
137 FALSE
138 }
139
140 check_name <- function(x,
141 ...,
142 allow_null = FALSE,
143 arg = caller_arg(x),
144 call = caller_env()) {
145 if (!missing(x)) {
146 is_string <- .rlang_check_is_string(
147 x,
148 allow_empty = FALSE,
149 allow_na = FALSE,
150 allow_null = allow_null
151 )
152 if (is_string) {
153 return(invisible(NULL))
154 }
155 }
156
157 stop_input_type(
158 x,
159 "a valid name",
160 ...,
161 allow_na = FALSE,
162 allow_null = allow_null,
163 arg = arg,
164 call = call
165 )
166 }
167
168 IS_NUMBER_true <- 0
169 IS_NUMBER_false <- 1
170 IS_NUMBER_oob <- 2
171
172 check_number_decimal <- function(x,
173 ...,
174 min = NULL,
175 max = NULL,
176 allow_infinite = TRUE,
177 allow_na = FALSE,
178 allow_null = FALSE,
179 arg = caller_arg(x),
180 call = caller_env()) {
181 if (missing(x)) {
182 exit_code <- IS_NUMBER_false
183 } else if (0 == (exit_code <- .standalone_types_check_dot_call(
184 ffi_standalone_check_number_1.0.7,
185 x,
186 allow_decimal = TRUE,
187 min,
188 max,
189 allow_infinite,
190 allow_na,
191 allow_null
192 ))) {
193 return(invisible(NULL))
194 }
195
196 .stop_not_number(
197 x,
198 ...,
199 exit_code = exit_code,
200 allow_decimal = TRUE,
201 min = min,
202 max = max,
203 allow_na = allow_na,
204 allow_null = allow_null,
205 arg = arg,
206 call = call
207 )
208 }
209
210 check_number_whole <- function(x,
211 ...,
212 min = NULL,
213 max = NULL,
214 allow_infinite = FALSE,
215 allow_na = FALSE,
216 allow_null = FALSE,
217 arg = caller_arg(x),
218 call = caller_env()) {
219 if (missing(x)) {
220 exit_code <- IS_NUMBER_false
221 } else if (0 == (exit_code <- .standalone_types_check_dot_call(
222 ffi_standalone_check_number_1.0.7,
223 x,
224 allow_decimal = FALSE,
225 min,
226 max,
227 allow_infinite,
228 allow_na,
229 allow_null
230 ))) {
231 return(invisible(NULL))
232 }
233
234 .stop_not_number(
235 x,
236 ...,
237 exit_code = exit_code,
238 allow_decimal = FALSE,
239 min = min,
240 max = max,
241 allow_na = allow_na,
242 allow_null = allow_null,
243 arg = arg,
244 call = call
245 )
246 }
247
248 .stop_not_number <- function(x,
249 ...,
250 exit_code,
251 allow_decimal,
252 min,
253 max,
254 allow_na,
255 allow_null,
256 arg,
257 call) {
258 if (allow_decimal) {
259 what <- "a number"
260 } else {
261 what <- "a whole number"
262 }
263
264 if (exit_code == IS_NUMBER_oob) {
265 min <- min %||% -Inf
266 max <- max %||% Inf
267
268 if (min > -Inf && max < Inf) {
269 what <- sprintf("%s between %s and %s", what, min, max)
270 } else if (x < min) {
271 what <- sprintf("%s larger than or equal to %s", what, min)
272 } else if (x > max) {
273 what <- sprintf("%s smaller than or equal to %s", what, max)
274 } else {
275 abort("Unexpected state in OOB check", .internal = TRUE)
276 }
277 }
278
279 stop_input_type(
280 x,
281 what,
282 ...,
283 allow_na = allow_na,
284 allow_null = allow_null,
285 arg = arg,
286 call = call
287 )
288 }
289
290 check_symbol <- function(x,
291 ...,
292 allow_null = FALSE,
293 arg = caller_arg(x),
294 call = caller_env()) {
295 if (!missing(x)) {
296 if (is_symbol(x)) {
297 return(invisible(NULL))
298 }
299 if (allow_null && is_null(x)) {
300 return(invisible(NULL))
301 }
302 }
303
304 stop_input_type(
305 x,
306 "a symbol",
307 ...,
308 allow_na = FALSE,
309 allow_null = allow_null,
310 arg = arg,
311 call = call
312 )
313 }
314
315 check_arg <- function(x,
316 ...,
317 allow_null = FALSE,
318 arg = caller_arg(x),
319 call = caller_env()) {
320 if (!missing(x)) {
321 if (is_symbol(x)) {
322 return(invisible(NULL))
323 }
324 if (allow_null && is_null(x)) {
325 return(invisible(NULL))
326 }
327 }
328
329 stop_input_type(
330 x,
331 "an argument name",
332 ...,
333 allow_na = FALSE,
334 allow_null = allow_null,
335 arg = arg,
336 call = call
337 )
338 }
339
340 check_call <- function(x,
341 ...,
342 allow_null = FALSE,
343 arg = caller_arg(x),
344 call = caller_env()) {
345 if (!missing(x)) {
346 if (is_call(x)) {
347 return(invisible(NULL))
348 }
349 if (allow_null && is_null(x)) {
350 return(invisible(NULL))
351 }
352 }
353
354 stop_input_type(
355 x,
356 "a defused call",
357 ...,
358 allow_na = FALSE,
359 allow_null = allow_null,
360 arg = arg,
361 call = call
362 )
363 }
364
365 check_environment <- function(x,
366 ...,
367 allow_null = FALSE,
368 arg = caller_arg(x),
369 call = caller_env()) {
370 if (!missing(x)) {
371 if (is_environment(x)) {
372 return(invisible(NULL))
373 }
374 if (allow_null && is_null(x)) {
375 return(invisible(NULL))
376 }
377 }
378
379 stop_input_type(
380 x,
381 "an environment",
382 ...,
383 allow_na = FALSE,
384 allow_null = allow_null,
385 arg = arg,
386 call = call
387 )
388 }
389
390 check_function <- function(x,
391 ...,
392 allow_null = FALSE,
393 arg = caller_arg(x),
394 call = caller_env()) {
395 if (!missing(x)) {
396 if (is_function(x)) {
397 return(invisible(NULL))
398 }
399 if (allow_null && is_null(x)) {
400 return(invisible(NULL))
401 }
402 }
403
404 stop_input_type(
405 x,
406 "a function",
407 ...,
408 allow_na = FALSE,
409 allow_null = allow_null,
410 arg = arg,
411 call = call
412 )
413 }
414
415 check_closure <- function(x,
416 ...,
417 allow_null = FALSE,
418 arg = caller_arg(x),
419 call = caller_env()) {
420 if (!missing(x)) {
421 if (is_closure(x)) {
422 return(invisible(NULL))
423 }
424 if (allow_null && is_null(x)) {
425 return(invisible(NULL))
426 }
427 }
428
429 stop_input_type(
430 x,
431 "an R function",
432 ...,
433 allow_na = FALSE,
434 allow_null = allow_null,
435 arg = arg,
436 call = call
437 )
438 }
439
440 check_formula <- function(x,
441 ...,
442 allow_null = FALSE,
443 arg = caller_arg(x),
444 call = caller_env()) {
445 if (!missing(x)) {
446 if (is_formula(x)) {
447 return(invisible(NULL))
448 }
449 if (allow_null && is_null(x)) {
450 return(invisible(NULL))
451 }
452 }
453
454 stop_input_type(
455 x,
456 "a formula",
457 ...,
458 allow_na = FALSE,
459 allow_null = allow_null,
460 arg = arg,
461 call = call
462 )
463 }
464
465
466 # Vectors -----------------------------------------------------------------
467
468 # TODO: Figure out what to do with logical `NA` and `allow_na = TRUE`
469
470 check_character <- function(x,
471 ...,
472 allow_na = TRUE,
473 allow_null = FALSE,
474 arg = caller_arg(x),
475 call = caller_env()) {
476
477 if (!missing(x)) {
478 if (is_character(x)) {
479 if (!allow_na && any(is.na(x))) {
480 abort(
481 sprintf("`%s` can't contain NA values.", arg),
482 arg = arg,
483 call = call
484 )
485 }
486
487 return(invisible(NULL))
488 }
489
490 if (allow_null && is_null(x)) {
491 return(invisible(NULL))
492 }
493 }
494
495 stop_input_type(
496 x,
497 "a character vector",
498 ...,
499 allow_null = allow_null,
500 arg = arg,
501 call = call
502 )
503 }
504
505 check_logical <- function(x,
506 ...,
507 allow_null = FALSE,
508 arg = caller_arg(x),
509 call = caller_env()) {
510 if (!missing(x)) {
511 if (is_logical(x)) {
512 return(invisible(NULL))
513 }
514 if (allow_null && is_null(x)) {
515 return(invisible(NULL))
516 }
517 }
518
519 stop_input_type(
520 x,
521 "a logical vector",
522 ...,
523 allow_na = FALSE,
524 allow_null = allow_null,
525 arg = arg,
526 call = call
527 )
528 }
529
530 check_data_frame <- function(x,
531 ...,
532 allow_null = FALSE,
533 arg = caller_arg(x),
534 call = caller_env()) {
535 if (!missing(x)) {
536 if (is.data.frame(x)) {
537 return(invisible(NULL))
538 }
539 if (allow_null && is_null(x)) {
540 return(invisible(NULL))
541 }
542 }
543
544 stop_input_type(
545 x,
546 "a data frame",
547 ...,
548 allow_null = allow_null,
549 arg = arg,
550 call = call
551 )
552 }
553
554 # nocov end