R/chk-s4-class.R

Defines functions vld_s4_class chk_s4_class

Documented in chk_s4_class vld_s4_class

#' Check Inherits from S4 Class
#'
#' @description
#' Checks inherits from S4 class using
#'
#' `isS4(x) && methods::is(x, class)`
#'
#' @inheritParams params
#' @inherit params return
#'
#' @family chk_is
#'
#' @examples
#' # chk_s4_class
#' try(chk_s4_class(1, "numeric"))
#' chk_s4_class(getClass("MethodDefinition"), "classRepresentation")
#' @export
chk_s4_class <- function(x, class, x_name = NULL) {
  if (vld_s4_class(x, class)) {
    return(invisible(x))
  }
  if (is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x))
  .class <- class
  abort_chk(x_name, " must inherit from S4 class '", .class, "'", x = x, .class = .class)
}

#' @describeIn chk_s4_class Validate Inherits from S4 Class
#'
#' @examples
#' # vld_s4_class
#' vld_s4_class(numeric(0), "numeric")
#' vld_s4_class(getClass("MethodDefinition"), "classRepresentation")
#' @export
vld_s4_class <- function(x, class) isS4(x) && methods::is(x, class)

Try the chk package in your browser

Any scripts or data that you put into this service are public.

chk documentation built on Oct. 6, 2023, 9:06 a.m.