R/emit.R

Defines functions emit

Documented in emit

#' Emits a plot to the viewer and to a file.
#' Writes plots to disk and to screen, fixed size 1024x768 PNG;
#' creates directory if necessary.
#' @param p the plot object
#' @param tag a name tag for the plot file, default \code{unknown}
#' @param path the disk path prefix, relative,
#' default \code{plots/}, must exist and be writable
#' @param prefix plot group name prefix, default \code{sc_}
#' and abbreviation for scorecard.
#' @param suffix plot name suffix, default \code{.png}.
#' @param show_warnings whether to show warnings on directory creation,
#' passed to \code{dir.create()}, default FALSE
#' @param recursive whether to create path elements other than last,
#' passed to \code{dir.create()}, default TRUE
#' @param width plot image width in pixels, default 2014
#' @param height plot image height in pixels, default 768
#' @return nothing
emit <- function(p,
                 tag = "uknnown",
                 path = "plots/",
                 prefix = "sc_",
                 suffix = ".png",
                 show_warnings = FALSE,
                 recursive = TRUE,
                 width = 1024,
                 height = 768) {
  if ("grob" %in% class(p)) {
    grid::grid.newpage()
    grid::grid.draw(p)
  } else {
    print(p)
  }
  dir.create(path, show_warnings, recursive)
  name <- paste(path, prefix, tag, suffix, sep = "")
  grDevices::dev.copy(png, width = width, height = height, name)
  grDevices::dev.off()
}
greatgray/scorecard documentation built on May 17, 2019, 8:34 a.m.