Merge pull request #679 from OscardR/master

Support propagation of parameters for `add_header_above` from calls to `header_separate`
diff --git a/R/mini_plots.R b/R/mini_plots.R
index 8576df0..137446e 100644
--- a/R/mini_plots.R
+++ b/R/mini_plots.R
@@ -196,6 +196,99 @@
     del = TRUE)
   return(out)
 }
+#' Helper functions to generate inline sparklines
+#'
+#' @description These functions helps you quickly generate sets of sparkline
+#' style plots using base R plotting system. Currently, we support histogram,
+#' boxplot, line, scatter, pointrange, barplot plots. You can use them together with
+#' `column_spec` to generate inline plot in tables. By default, this function
+#' will save images in a folder called "kableExtra" and return the address of
+#' the file.
+#'
+#' @param x Vector of values or List of vectors of values.
+#' @param width The width of the plot in pixel
+#' @param height The height of the plot in pixel
+#' @param res The resolution of the plot. Default is 300.
+#' @param add_label For boxplot. T/F to add labels for min, mean and max.
+#' @param label_digits If T for add_label, rounding digits for the label.
+#' Default is 2.
+#' @param same_lim T/F. If x is a list of vectors, should all the plots be
+#' plotted in the same range? Default is True.
+#' @param lim Manually specify plotting range in the form of
+#' `c(0, 10)`.
+#' @param xaxt On/Off for xaxis text
+#' @param yaxt On/Off for yaxis text
+#' @param ann On/Off for annotations (titles and axis titles)
+#' @param col Color for the fill of the histogram bar/boxplot box.
+#' @param border Color for the border.
+#' @param boxlty Boxplot - box boarder type
+#' @param medcol Boxplot - median line color
+#' @param medlwd Boxplot - median line width
+#' @param dir Directory of where the images will be saved.
+#' @param file File name. If not provided, a random name will be used
+#' @param file_type Graphic device. Can be character (e.g., `"pdf"`)
+#'   or a graphics device function (`grDevices::pdf`). This defaults
+#'   to `"pdf"` if the rendering is in LaTeX and `"svg"` otherwise.
+#' @param ... extraparameters passing to boxplot
+#'
+#' @export
+spec_barplot <- function(x, devwidth = 200, devheight = 40, res = 300,
+                      beside = F,
+                      horiz = F,
+                      same_lim = TRUE, lim = NULL,
+                      xaxt = 'n', yaxt = 'n', ann = FALSE,
+                      col = NULL, border = NA,
+                      dir = if (is_latex()) rmd_files_dir() else tempdir(),
+                      file = NULL,
+                      file_type = if (is_latex()) "pdf" else svglite::svglite,
+                      ...) {
+  if (is.list(x)) {
+    if (same_lim & is.null(lim)) {
+      lim <- base::range(unlist(x), na.rm=TRUE)
+    }
+    
+    dots <- listify_args(x, devwidth, devheight, res, beside,horiz,
+                         lim, xaxt, yaxt, ann, col, border,
+                         dir, file, file_type,
+                         lengths = c(1, length(x)))
+    return(do.call(Map, c(list(f = spec_barplot), dots)))
+  }
+  
+  if (is.null(x)) return(NULL)
+  
+  if (is.null(lim)) {
+    lim <- base::range(x, na.rm=TRUE)
+  }
+  
+  if (!dir.exists(dir)) {
+    dir.create(dir)
+  }
+  height<-matrix(x)
+  height<-cbind(height,0)
+  file_ext <- dev_chr(file_type)
+  if (is.null(file)) {
+    file <- normalizePath(
+      tempfile(pattern = "barplot_", tmpdir = dir, fileext = paste0(".", file_ext)),
+      winslash = "/", mustWork = FALSE)
+  }
+  
+  graphics_dev(filename = file, dev = file_type,
+               width = devwidth, height = devheight, res = res,
+               bg = "transparent")
+  curdev <- grDevices::dev.cur()
+  on.exit(grDevices::dev.off(curdev), add = TRUE)
+  
+  graphics::par(mar = c(0, 0, 0, 0), lwd=0.5)
+  graphics::barplot(height=height, beside = beside,horiz = horiz, col = col, border = border,xaxt = xaxt, yaxt = yaxt, ann = ann)#,xlim = lim, ann = ann, ...)
+  
+  grDevices::dev.off(curdev)
+
+  out <- make_inline_plot(
+    file, file_ext, file_type,
+    devwidth, devheight, res,
+    del = TRUE)
+  return(out)
+}
 
 is_latex <- knitr::is_latex_output