R/chk-s3-class.R

Defines functions vld_s3_class chk_s3_class

Documented in chk_s3_class vld_s3_class

#' Check Type
#'
#' @description
#' Checks inherits from S3 class using
#'
#' `!isS4(x) && inherits(x, class)`
#'
#' @inheritParams params
#' @inherit params return
#'
#' @family chk_is
#'
#' @examples
#' # chk_s3_class
#' chk_s3_class(1, "numeric")
#' try(chk_s3_class(getClass("MethodDefinition"), "classRepresentation"))
#' @export
chk_s3_class <- function(x, class, x_name = NULL) {
  if (vld_s3_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 S3 class '", .class, "'", x = x, .class = .class)
}

#' @describeIn chk_s3_class Validate Inherits from S3 Class
#'
#' @examples
#' # vld_s3_class
#' vld_s3_class(numeric(0), "numeric")
#' vld_s3_class(getClass("MethodDefinition"), "classRepresentation")
#' @export
vld_s3_class <- function(x, class) !isS4(x) && inherits(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.