R/print.R

Defines functions pretty.print.string pretty.print.bold.string print.empty.line pretty.print.vector.names pretty.print.vector.values pretty.print.vector.names.and.values pretty.print.name.and.value

Documented in pretty.print.bold.string pretty.print.string pretty.print.vector.names pretty.print.vector.names.and.values pretty.print.vector.values

#' nice printing of a string in an R notebook
#' use `with.gt = FALSE` when you want to nicely print multiple lines
#' ('gt' is the '>' symbol)
#' @export
pretty.print.string = function(string, with.gt = TRUE) {
  if (with.gt)
    cat(paste0("> ", string))
  else
    cat(string)
}

#' prints a bold string only when `html.output` is enabled. Otherwise,
#' prints a normal string
#' @export
pretty.print.bold.string =
  function(string, with.gt = TRUE, html.output = TRUE) {
    if (html.output) {
      bold.string = paste0("<b>", string, "</b>")
      if (with.gt)
        cat(paste0("> ", bold.string))
      else
        cat(bold.string)
    } else {
      pretty.print.string(string, with.gt = with.gt)
    }
}

#' @export
print.empty.line = function(html.output = FALSE) {
  if (html.output)
    cat("<br/>")
  else
    cat("\n")
}

#' `vector.names.str` tell us what `names(vec)` actually is, to put it on
#' the print message
#' @export
pretty.print.vector.names = function(vec, vector.names.str = "nodes",
                                     seperator = ", ", with.gt = TRUE) {
  if (length(vec) == 1) {
    vector.names.str = substr(vector.names.str, start = 1,
                              stop = nchar(vector.names.str) - 1)
  }
  pretty.print.string(paste0(length(vec), " ", vector.names.str, ": ",
                      paste0(names(vec), collapse = seperator)), with.gt)
}

#' `vector.values.str` tell us what the `vec` values are, to put it on
#' the print message
#' @export
pretty.print.vector.values = function(vec, vector.values.str = "nodes",
                                      seperator = ", ", with.gt = TRUE) {
  if (length(vec) == 1) {
    vector.values.str = substr(vector.values.str, start = 1,
                              stop = nchar(vector.values.str) - 1)
  }
  pretty.print.string(paste0(length(vec), " ", vector.values.str, ": ",
                      paste0(vec, collapse = seperator)), with.gt)
}

#' vector `vec` has to have `names(vec)` non-empty
#' `n` is the (integer) number of elements you want to print
#' @export
pretty.print.vector.names.and.values = function(vec, n = -1) {
  len = length(vec)
  stopifnot(len > 0)

  # print all elements by default
  if (n == -1) n = len

  vec.names = names(vec)
  if (len == 1) {
    pretty.print.name.and.value(vec.names, vec, with.gt = TRUE, with.comma = FALSE)
  } else {
    # limit elements to show
    if (n >= 1 & n < len)
      last.index = n
    else
      last.index = len

    for (index in 1:last.index) {
      name = vec.names[index]
      value = vec[index]
      if (index == 1 & index != last.index)
        pretty.print.name.and.value(name, value, with.gt = TRUE, with.comma = TRUE)
      if (index == 1 & index == last.index)
        pretty.print.name.and.value(name, value, with.gt = TRUE, with.comma = FALSE)
      if (index != 1 & index != last.index)
        pretty.print.name.and.value(name, value)
      if (index != 1 & index == last.index)
        pretty.print.name.and.value(name, value, with.comma = FALSE)
    }
  }
}

#' @export
pretty.print.name.and.value =
  function(name, value, with.gt = FALSE, with.comma = TRUE) {
    if (with.comma) {
      pretty.print.string(string = paste0(name, ": ", value, ", "),
                          with.gt = with.gt)
    }
    else
      pretty.print.string(string = paste0(name, ": ", value), with.gt = with.gt)
}
bblodfon/xxxfun documentation built on May 29, 2019, 12:01 a.m.