R/has-terms.R

Defines functions has_terms

Documented in has_terms

#' Does the input have terms?
#'
#' Checks to see if the input has a terms component or attribute.
#'
#' @param x Input to check.
#' @param .xname Not intended to be used directly.
#' @param severity How severe should the consequences of the assertion be?  
#' Either \code{"stop"}, \code{"warning"}, \code{"message"}, or \code{"none"}.
#' @return \code{has_terms} returns \code{TRUE} if the input has an 
#' element or an attribute named terms. \code{assert_has_terms} returns 
#' nothing but throws an error if \code{has_terms} is not \code{TRUE}.
#' @seealso \code{\link[stats]{terms}}.
#' @examples
#' model <- lm(uptake ~ conc, datasets::CO2)
#' # this works because model$terms is not null
#' assert_has_terms(model)
#' @export
has_terms <- function(x, .xname = get_name_in_parent(x))
{
  if(
    is.null(attr(x, "terms")) && 
    (is.atomic(x) || is.null(x$terms))
  )
  {
    return(
      false(
        gettext("%s has no terms component nor attribute."), 
        .xname
      )
    )
  }
  TRUE
}

Try the assertive.models package in your browser

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

assertive.models documentation built on May 2, 2019, 10:24 a.m.