R/utils.R

Defines functions init_auto_index text_wrap is_empty plot.consort

Documented in plot.consort

#' Add methods to print function
#'
#' Method for plot objects and display the output in on a grid device.
#'
#' @param x A \code{consort} object.
#' @param grViz If use \link[DiagrammeR]{grViz} to print the plot. 
#' Default is \code{FALSE} to use \link[grid]{grid.draw}
#' @param ... Not used.
#'
#' @seealso \code{\link{add_side_box}},\code{\link{add_split}},
#' \code{\link{add_side_box}}, \link[grid]{grid.draw}
#'
#' @return None.
#'
#' @rdname plot.consort
#' @export
plot.consort <- function(x, grViz = FALSE, ...) {
  if(!grViz){
    r <- build_grid(x)
    grid.newpage()
    grid.draw(r)
  }else{
    if (requireNamespace("DiagrammeR", quietly = TRUE)) {
      grviz_txt <- build_grviz(x)
      DiagrammeR::grViz(grviz_txt)
    } else {
      stop("package `DiagrammeR` is needed to draw grViz plot.")
    }
  }
}

#' @rdname plot.consort
#' @export
print.consort <- plot.consort

# Check if empty
#' @keywords internal
#'
is_empty <- function(x){
  x <- gsub("[[:space:]]", "", x)
  is.null(x) | x == "" | is.na(x)
}

# Wrap text
#' @keywords internal
#'
text_wrap <- function(txt, width = 0.9 * getOption("width")) {
  if (length(txt) > 1) {
    stop("Vector does not supported!")
  }

  # Split text by line break
  s_txt <- unlist(strsplit(txt, split = "\n"))
  if (requireNamespace("stringi", quietly = TRUE)) {
    s_txt <- stringi::stri_wrap(s_txt, width, 0)
  } else {
    s_txt <- strwrap(s_txt, width)
  }
  paste(s_txt, collapse = "\n")
}

#' @keywords internal
init_auto_index <- function() {
  index <- 0
  function() {
    index <<- index + 1
    index
  }
}

auto_index <- init_auto_index()

Try the consort package in your browser

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

consort documentation built on Sept. 23, 2023, 1:06 a.m.