R/print_methods.R

Defines functions print.infer_dist print.infer_layer print.infer

Documented in print.infer print.infer_dist print.infer_layer

#' Print methods
#'
#' @param x An object of class `infer`, i.e. output from [specify()] or
#'   [hypothesize()], or of class `infer_layer`, i.e. output from
#'   [shade_p_value()] or [shade_confidence_interval()].
#' @param ... Arguments passed to methods.
#' @importFrom glue glue_collapse glue
#'
#' @rdname print.infer
#' @export
print.infer <- function(x, ...) {
  attrs <- names(attributes(x))
  header <- character(3)
  if ("response" %in% attrs) {
    header[1] <- glue(
      'Response: {response_name(x)} ({attr(x, "response_type")})',
      .null = "NULL"
    )
    if ("explanatory" %in% attrs) {
      header[2] <- glue(
        'Explanatory: {paste0(paste0(explanatory_name(x), " (",
        attr(x, "explanatory_type"), ")"), collapse = ", ")}',
        .null = "NULL"
      )
    }
  }
  if ("null" %in% attrs) {
    header[3] <- glue('Null Hypothesis: {attr(x, "null")}', .null = "NULL")
  }

  cat(glue::glue_collapse(header[header != ""], sep = "\n"))
  cat("\n")

  NextMethod()
}

#' @rdname print.infer
#' @export
print.infer_layer <- function(x, ...) {
  cat(x)
}

#' @rdname print.infer
#' @export
print.infer_dist <- function(x, ...) {
  cat(x)
}

Try the infer package in your browser

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

infer documentation built on Sept. 8, 2023, 6:22 p.m.