/
chk-whole-numeric.R
45 lines (44 loc) · 1.04 KB
/
chk-whole-numeric.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
#' Check Whole Numeric
#'
#' @description
#' Checks if integer vector or double equivalent using
#'
#' `is.integer(x) || (is.double(x) && vld_true(all.equal(x, as.integer(x))))`
#'
#'
#' @inheritParams params
#' @inherit params return
#'
#' @family chk_is
#'
#' @examples
#' # chk_whole_numeric
#' chk_whole_numeric(1)
#' try(chk_whole_numeric(1.1))
#' @export
chk_whole_numeric <- function(x, x_name = NULL) {
if (vld_whole_numeric(x)) {
return(invisible(x))
}
if (is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x))
abort_chk(
x_name,
" must be a whole numeric vector (integer vector or double equivalent)",
x = x
)
}
#' @describeIn chk_whole_numeric Validate Whole Numeric
#'
#' @examples
#' # vld_whole_numeric
#' vld_whole_numeric(1)
#' vld_whole_numeric(NA_real_)
#' vld_whole_numeric(1:2)
#' vld_whole_numeric(double(0))
#' vld_whole_numeric(TRUE)
#' vld_whole_numeric(1.5)
#' @export
vld_whole_numeric <- function(x) {
is.integer(x) || (is.double(x) &&
vld_true(all.equal(x[!is.na(x)], trunc(x[!is.na(x)]))))
}