Skip to content

Commit 1330caf

Browse files
committed
init type_check
1 parent a20e672 commit 1330caf

File tree

5 files changed

+180
-0
lines changed

5 files changed

+180
-0
lines changed

R/type_check.R

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# type_check.R
2+
# ::rtemis::
3+
# 2024 EDG rtemis.org
4+
5+
#' Check type of object
6+
#'
7+
#' @param x Object to check
8+
#' @param fn Function to check against, any `is.*` function, e.g. `is.character`
9+
#'
10+
#' @return Logical
11+
#' @author EDG
12+
#' @keywords internal
13+
#' @examples
14+
#' is_check("papaya", is.character) # TRUE
15+
#' is_check(c(1, 2.5, 3.2), is.integer) # FALSE
16+
#' is_check(iris, is.list) # TRUE
17+
18+
is_check <- function(x, fn) {
19+
if (!fn(x)) {
20+
input <- deparse(substitute(x))
21+
type <- substr(deparse(substitute(fn)), 4, 99)
22+
message(red(bold(input), "is not", bold(type)))
23+
return(FALSE)
24+
}
25+
return(TRUE)
26+
} # /rtemis::is_check
27+
28+
29+
#' Test type of object
30+
#'
31+
#' @inheritParams is_check
32+
#'
33+
#' @return NULL (invisibly)
34+
#' @author EDG
35+
#' @keywords internal
36+
37+
is_test <- function(x, fn) {
38+
if (!is.null(x) && !fn(x)) {
39+
input <- deparse(substitute(x))
40+
type <- substr(deparse(substitute(fn)), 4, 99)
41+
stop(bold(input), " is not ", bold(type))
42+
}
43+
invisible(NULL)
44+
} # /rtemis::is_test
45+
46+
47+
#' Check class of object
48+
#'
49+
#' @param x Object to check
50+
#' @param cl Character: class to check against
51+
#'
52+
#' @return Logical
53+
#' @author EDG
54+
#' @keywords internal
55+
#' @examples
56+
#' inherits_check("papaya", "character") # TRUE
57+
#' inherits_check(c(1, 2.5, 3.2), "integer") # FALSE
58+
#' inherits_check(iris, "list") # FALSE, compare to is_check(iris, is.list)
59+
60+
inherits_check <- function(x, cl) {
61+
if (!inherits(x, cl)) {
62+
input <- deparse(substitute(x))
63+
message(red(bold(input), "is not", bold(cl)))
64+
return(FALSE)
65+
}
66+
return(TRUE)
67+
} # /rtemis::inherits_check
68+
69+
70+
#' Test class of object
71+
#'
72+
#' @inheritParams inherits_check
73+
#'
74+
#' @return NULL (invisibly)
75+
#' @author EDG
76+
#' @keywords internal
77+
78+
inherits_test <- function(x, cl) {
79+
if (!is.null(x) && !inherits(x, cl)) {
80+
input <- deparse(substitute(x))
81+
stop(bold(input), " is not ", bold(cl))
82+
}
83+
invisible(NULL)
84+
} # /rtemis::inherits_test

man/inherits_check.Rd

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/inherits_test.Rd

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/is_check.Rd

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/is_test.Rd

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)