R/object-format.R

#' Default format for data
#'
#' This function is called to generate the default "Format" section for each
#' data object.  The default implementation will return the class and dimension
#' information.
#'
#' @param x A data object
#' @return A `character` value with valid `Rd` syntax, or `NULL`.
#' @export
object_format <- function(x) {
  UseMethod("object_format")
}

#' @export
object_format.default <- function(x) {
  paste0("An object of class ", format_classes(x), " ", format_dim(x), ".")
}

format_classes <- function(x) {
  classes <- paste0("\\code{", class(x), "}")

  base_classes <- NULL
  if (length(classes) > 1L) {
    base_classes <- paste0(
      " (inherits from ",
      paste(classes[-1L], collapse = ", "),
      ")")
  }

  paste0(classes[[1L]], base_classes)
}

format_dim <- function(x) {
  if (length(dim(x)) == 2L) {
    paste0("with ", nrow(x), " rows and ", ncol(x), " columns")
  } else if (length(dim(x)) > 2L) {
    paste0("of dimension ", paste(dim(x), collapse = " x "))
  } else {
    paste0("of length ", length(x))
  }
}
klmr/roxydoxy documentation built on May 20, 2019, 4:09 p.m.