Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,13 @@ export(chk_off)
export(chk_on)
export(chk_orderset)
export(chk_proportion)
export(chk_r6_class)
export(chk_range)
export(chk_raw)
export(chk_s3_class)
export(chk_s3_class_strict)
export(chk_s4_class)
export(chk_s7_class)
export(chk_scalar)
export(chk_setequal)
export(chk_sorted)
Expand Down Expand Up @@ -167,11 +169,13 @@ export(vld_null)
export(vld_number)
export(vld_numeric)
export(vld_orderset)
export(vld_r6_class)
export(vld_range)
export(vld_raw)
export(vld_s3_class)
export(vld_s3_class_strict)
export(vld_s4_class)
export(vld_s7_class)
export(vld_scalar)
export(vld_setequal)
export(vld_sorted)
Expand Down
60 changes: 60 additions & 0 deletions R/chk-r6-class.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#' Check Inherits from R6 Class
#'
#' @description
#' Checks inherits from R6 class using
#'
#' `inherits(x, "R6") && inherits(x, class)`
#'
#' @details
#' All R6 objects inherit from the class `'R6'`, so `chk_r6_class(x, "R6")`
#' checks that `x` is an R6 object of any class.
#' R6 class generators (as returned by [R6::R6Class()]) are not themselves
#' R6 objects.
#'
#' @inheritParams params
#' @inherit params return
#'
#' @family id_checkers
#'
#' @seealso [inherits()]
#' @seealso For more details about the use of this function,
#' please read the article
#' `vignette("chk-families")`.
#'
#' @examplesIf requireNamespace("R6", quietly = TRUE)
#' # chk_r6_class
#' chk_r6_class(R6::R6Class("exampleR6class")$new(), "exampleR6class")
#' try(chk_r6_class(1, "numeric"))
#' @export
chk_r6_class <- function(x, class, x_name = NULL) {
if (vld_r6_class(x, class)) {
return(invisible(x))
}
if (is.null(x_name)) {
x_name <- deparse_backtick_chk(substitute(x))
}
.class <- cc(class, conj = " or ", chk = FALSE)
abort_chk(
x_name,
" must inherit from R6 class",
if (length(class) == 1) " " else "es ",
.class,
", not ",
object_type(x),
" class",
if (length(class(x)) == 1) " " else "es ",
cc(class(x), conj = " and "),
".",
x = x,
.class = .class
)
}

#' @describeIn chk_r6_class Validate Inherits from R6 Class
#'
#' @examplesIf requireNamespace("R6", quietly = TRUE)
#' # vld_r6_class
#' vld_r6_class(R6::R6Class("exampleR6class")$new(), "exampleR6class")
#' vld_r6_class(1, "numeric")
#' @export
vld_r6_class <- function(x, class) inherits(x, "R6") && inherits(x, class)
8 changes: 5 additions & 3 deletions R/chk-s3-class.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ chk_s3_class <- function(x, class, x_name = NULL) {
.class <- cc(class, conj = " or ", chk = FALSE)
abort_chk(
x_name,
" must inherit from S3 class", if (length(class) == 1) " " else "es ",
" must inherit from S3 class",
if (length(class) == 1) " " else "es ",
.class,
", not ",
if (vld_s4_class(x, class(x))) "S4" else if (inherits(x, "R6")) "R6" else "S3",
" class", if (length(class(x)) == 1) " " else "es ",
object_type(x),
" class",
if (length(class(x)) == 1) " " else "es ",
cc(class(x), conj = " and "),
".",
x = x,
Expand Down
12 changes: 8 additions & 4 deletions R/chk-s4-class.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ chk_s4_class <- function(x, class, x_name = NULL) {
.class <- cc(class, conj = " or ", chk = FALSE)
abort_chk(
x_name,
" must inherit from S4 class", if (length(class) == 1) " " else "es ",
" must inherit from S4 class",
if (length(class) == 1) " " else "es ",
.class,
", not ",
if (vld_s4_class(x, class(x))) "S4" else if (inherits(x, "R6")) "R6" else "S3",
" class", if (length(class(x)) == 1) " " else "es ",
object_type(x),
" class",
if (length(class(x)) == 1) " " else "es ",
cc(class(x), conj = " and "),
".",
x = x,
Expand All @@ -49,4 +51,6 @@ chk_s4_class <- function(x, class, x_name = NULL) {
#' vld_s4_class(numeric(0), "numeric")
#' vld_s4_class(getClass("MethodDefinition"), "classRepresentation")
#' @export
vld_s4_class <- function(x, class) isS4(x) && any(sapply(class, \(.c) methods::is(x, .c)))
vld_s4_class <- function(x, class) {
isS4(x) && any(sapply(class, \(.c) methods::is(x, .c)))
}
72 changes: 72 additions & 0 deletions R/chk-s7-class.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#' Check Inherits from S7 Class
#'
#' @description
#' Checks inherits from S7 class using
#'
#' `inherits(x, "S7_object") && inherits(x, class)`
#'
#' @details
#' All S7 objects inherit from the class `'S7_object'`, so
#' `chk_s7_class(x, "S7_object")` checks that `x` is an S7 object of any class.
#'
#' S7 is self-describing, so an S7 class generator (as returned by
#' [S7::new_class()]) is itself an S7 object, of class `'S7_class'`.
#' It is not an object of the class it generates.
#'
#' `class` is a character vector of class names matching those in `class(x)`.
#' S7 qualifies class names with the package they are defined in, so an S7
#' class defined in package `foo` is matched by `'foo::ClassName'` and not by
#' `'ClassName'`.
#' S7 class objects are not accepted as `class`.
#'
#' @inheritParams params
#' @inherit params return
#'
#' @family id_checkers
#'
#' @seealso [inherits()]
#' @seealso For more details about the use of this function,
#' please read the article
#' `vignette("chk-families")`.
#'
#' @examplesIf requireNamespace("S7", quietly = TRUE)
#' # chk_s7_class
#' Foo <- S7::new_class("Foo", package = "mypkg")
#' chk_s7_class(Foo(), "mypkg::Foo")
#' try(chk_s7_class(1, "numeric"))
#' @export
chk_s7_class <- function(x, class, x_name = NULL) {
if (vld_s7_class(x, class)) {
return(invisible(x))
}
if (is.null(x_name)) {
x_name <- deparse_backtick_chk(substitute(x))
}
.class <- cc(class, conj = " or ", chk = FALSE)
abort_chk(
x_name,
" must inherit from S7 class",
if (length(class) == 1) " " else "es ",
.class,
", not ",
object_type(x),
" class",
if (length(class(x)) == 1) " " else "es ",
cc(class(x), conj = " and "),
".",
x = x,
.class = .class
)
}

#' @describeIn chk_s7_class Validate Inherits from S7 Class
#'
#' @examplesIf requireNamespace("S7", quietly = TRUE)
#' # vld_s7_class
#' Foo <- S7::new_class("Foo", package = "mypkg")
#' vld_s7_class(Foo(), "mypkg::Foo")
#' vld_s7_class(1, "numeric")
#' @export
vld_s7_class <- function(x, class) {
inherits(x, "S7_object") && inherits(x, class)
}
14 changes: 14 additions & 0 deletions R/internal.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,17 @@

anyDuplicated(x)
}

# the class system an object belongs to, for reporting in error messages
# R5 reference classes count as S4; base objects count as S3
object_type <- function(x) {
if (vld_s4_class(x, class(x))) {
"S4"
} else if (inherits(x, "R6")) {
"R6"
} else if (inherits(x, "S7_object")) {
"S7"
} else {
"S3"
}
}
2 changes: 2 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,11 @@ reference:
contents:
- chk_is
- chk_class
- chk_r6_class
- chk_s3_class
- chk_s3_class_strict
- chk_s4_class
- chk_s7_class
- title: REGEX Checker
desc: Check if the function input matches a REGEX
contents:
Expand Down
4 changes: 3 additions & 1 deletion man/chk_class.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion man/chk_data.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion man/chk_is.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 69 additions & 0 deletions man/chk_r6_class.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion man/chk_s3_class.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion man/chk_s3_class_strict.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion man/chk_s4_class.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading