Merge pull request #529 from r2evans/add/524_pdf

add pdf and general graphics-device support
diff --git a/R/graphics_helpers.R b/R/graphics_helpers.R
new file mode 100644
index 0000000..83c113a
--- /dev/null
+++ b/R/graphics_helpers.R
@@ -0,0 +1,219 @@
+#' Helper functions to use various graphics devices
+#'
+#' These helper functions generalize the use of strings (e.g.,
+#' `"svg"`, `"pdf"`) or graphic device functions (e.g.,
+#' `grDevices::svg`, `grDevices::pdf`) for in-table plots.
+#'
+#' @param filename Passed through to the graphics device.
+#' @param width,height Plot dimensions in pixels.
+#' @param res The resolution of the plot; default is 300.
+#' @param ... extra parameters passing to the graphics-device function.
+#' @param dev Character (e.g., "svg", "pdf") or function (e.g.,
+#' `grDevices::svg`, `grDevices::pdf`).
+#' @name graphics_helpers
+NULL
+
+#' @describeIn graphics_helpers Generalize 'res' and 'filename across dev functions
+#' @details
+#' - `graphics_dev` generalizes the use of 'res' and plot dimensions
+#'   across graphic devices. Raster-based devices (e.g., 'png',
+#'   'jpeg', 'tiff', 'bmp') tend to use 'res' and the width/height
+#'   units default to pixels. All other devices (e.g., 'pdf', 'svg')
+#'   tend to use inches as the default units for width/height, and
+#'   error when 'res' is provided.
+#'
+#'   The current heuristic is the look for the 'res' argument in the
+#'   function's formals; if that is present, then it is assumed that
+#'   the default units are in pixels, so 'width', 'height', and 'res'
+#'   are passed through unmodified. If 'res' is not present, then
+#'   'width' and 'height' are converted from pixels to inches, and
+#'   'res' is not passed to the function
+#'
+#'   Another purpose of this function is to generalize the different
+#'   graphic functions' use of 'file=' versus 'filename='.
+#' @return 'graphics_dev': nothing, a plot device is opened
+graphics_dev <- function(filename, width, height, res, ..., dev) {
+  dev <- match.fun(dev)
+  frmls <- names(formals(dev))
+  dots <- list(...)
+  if ("res" %in% frmls) {
+    dots <- c(dots, list(width = width, height = height, res = res))
+  } else {
+    dots <- c(dots, list(width = width / res, height = height / res))
+  }
+  filenames <- c("file", "filename")
+  found <- na.omit(match(frmls, filenames))
+  if (length(found)) {
+    dots <- c(dots, setNames(filename, filenames[ found ]))
+  } else {
+    stop("could not find a 'file' argument in graphics dev")
+  }
+  do.call(dev, dots)
+}
+
+#' @describeIn graphics_helpers Determine if plot device is svg-like
+#' @details
+#' - `is_svg` determines if the plot device is svg-like, typically one
+#'   of `"svg", `grDevices::svg`, or `svglite::svglite`
+#' @return 'is_svg': logical
+is_svg <- function(dev) {
+  if (is.character(dev)) {
+    return(grepl("svg", dev))
+  }
+  if (is.function(dev)) {
+    return(any(sapply(formals(dev), function(f) {
+      tryCatch(any(grepl("svg", as.character(f))),
+               error = function(e) FALSE)
+    })))
+  }
+  stop("unrecognized graphics 'dev': ", paste(class(dev), collapse = ","))
+}
+
+#' @describeIn graphics_helpers Determine filename extension
+#' @details
+#'
+#' - `dev_chr` determines the filename extension for the applicable
+#'   plot function; when `dev` is a string, then it is returned
+#'   unchanged; when `dev` is a function, the formals of the function
+#'   are checked for clues (i.e., default value of a `file=` argument)
+#' @return `dev_chr`: character
+#' @importFrom tools file_ext
+dev_chr <- function(dev) {
+  ext <- ""
+  if (is.character(dev)) {
+    ext <- if (dev == "svglite") "svg" else dev
+  } else if (is.function(dev)) {
+    frmls <- formals(dev)
+    filearg <- grep("^file(name)?$", names(frmls), value = TRUE)
+    if (length(filearg)) {
+      ext <- grep("\\.[[:alpha:]]+$", unlist(sapply(frmls[filearg], as.character)),
+                  value = TRUE)
+      ext <- unique(tools::file_ext(ext))[1]
+    }
+  }
+  if (is.na(ext) || !nzchar(ext)) {
+    warning("could not determine filename extension from graphic device")
+    ext <- ""
+  }
+  return(ext)
+}
+
+#' Combine file (or svg text) and parameters into a 'kableExtraInlinePlots' object
+#' 
+#' @param filename Passed through to the graphics device.
+#' @param file_ext Character, something like "png".
+#' @param dev Character (e.g., "svg", "pdf") or function (e.g.,
+#' @param width,height Plot dimensions in pixels.
+#' @param res The resolution of the plot; default is 300.
+#' @param del If the file is svg-like, then the default action is to
+#'   read the file into an embedded SVG object; once done, the file is
+#'   no longer used. The default action is to delete this file early,
+#'   set this to 'FALSE' to keep the file.
+#' @return list object, with class 'kableExtraInlinePlots'
+make_inline_plot <- function(filename, file_ext, dev,
+                             width, height, res,
+                             del = TRUE) {
+  if (is_latex() && (is_svg(file_ext) || is_svg(dev))) {
+    svg_xml <- xml2::read_xml(filename)
+    svg_text <- as.character(svg_xml)
+    if (del) {
+      unlink(filename)
+      filename <- character(0)
+    }
+  } else {
+    if (!is_latex()) {
+      filename <- paste0("file:///", normalizePath(filename, winslash = "/"))
+    }
+    svg_text <- NULL
+  }
+  out <- list(path = filename, dev = file_ext, type = "line",
+              width = width, height = height, res = res,
+              svg_text = svg_text)
+  class(out) <- c("kableExtraInlinePlots", "list")
+  return(out)
+}
+
+#' Convert arguments for a single call into Map-able args
+#'
+#' @param ... Arbitrary arguments to be possibly converted into lists
+#'   of arguments.
+#' @param lengths Allowable lengths of the arguments, typically 1 and
+#'   the length of the main variable (e.g., "x"). If 'NA' (default),
+#'   it is not enforced.
+#' @param passthru Character vector of variables to pass through with
+#'   no conversion to lists of values. Extra names (not provided in
+#'   `...`) are ignored.
+#' @param notlen1vec Character vector of variables that are known to
+#'   be length over 1 for a single plot call, so it will always be
+#'   list-ified and extra care to ensure it is grouped correctly.
+#'   Extra names (not provided in `...`) are ignored.
+#' @param notlen1lst Character vector of variables that are lists, so
+#'   the inner list length is not checked/enforced. (For example, if a
+#'   single plot call takes an argument `list(a=1,b=2,d=3)` and the
+#'   multi-data call creates three plots, then a naive match might
+#'   think that the first plot would get `list(a=1)`, second plot gets
+#'   `list(b=2)`, etc. Adding that list-argument to this 'notlen1lst'
+#'   will ensure that the full list is passed correctly.) Extra names
+#'   (not provided in `...`) are ignored.
+#' @param ignore Character vector of variables to ignore, never
+#'   returned. (Generally one can control this by not adding the
+#'   variable in the first place, but having this here allows some
+#'   sanity checks and/or programmatic usage.)
+#' @return list, generally a list of embedded lists
+listify_args <- function(..., lengths = NA,
+                         passthru = c("x", "y"),
+                         notlen1vec = c("lim", "xlim", "ylim"),
+                         notlen1lst = c("minmax", "min", "max"),
+                         ignore = c("same_lim")) {
+  indots <- list(...)
+  dotnms <- sapply(match.call(expand.dots=FALSE)$..., deparse)
+  neednames <- if (is.null(names(indots))) {
+    rep(TRUE, length(indots))
+  } else !nzchar(names(indots))
+  if (any(neednames)) {
+    names(indots)[ neednames ] <- dotnms[ neednames ]
+  }
+  dots <- indots[ intersect(names(indots), passthru) ]
+
+  # these are elements that are not typically length-1, so we need to
+  # listify slightly differently
+  nms <- intersect(names(indots), notlen1vec)
+  if (length(nms)) {
+    dots <- c(dots, Map(
+      function(L, nm) {
+        if (is.null(L)) return(list(NULL))
+        if (!is.list(L)) return(list(L))
+        if (is.na(lengths) || length(L) %in% lengths) return(L)
+        stop("length of '", nm, "' must be one of: ", paste(lengths, collapse = " or "))
+      }, indots[ nms ], nms))
+  }
+
+  # these are a little special in that the argument must be a list
+  # (regardless of its internal length)
+  nms <- intersect(names(indots), notlen1lst)
+  if (length(nms)) {
+    dots <- c(dots, Map(
+      function(L, nm) {
+        if (is.null(L)) return(list(NULL))
+        if (!length(L)) return(list(list()))
+        if (!is.list(L[[1]])) return (list(L))
+        if (is.na(lengths) || length(L) %in% lengths) return(L)
+        stop("length of '", nm, "' must be one of: ", paste(lengths, collapse = " or "))
+      }, indots[ nms ], nms))
+  }
+
+  # the remainder, those that we don't know about explicitly and are
+  # not intentionally ignored
+  nms <- setdiff(names(indots), c(passthru, notlen1vec, notlen1lst, ignore))
+  if (length(nms)) {
+    dots <- c(dots, Map(
+      function(V, nm) {
+        if (is.null(V)) return(list(NULL))
+        if (is.function(V)) return(list(V))
+        if (is.na(lengths) || length(V) %in% lengths) return(V)
+        stop("length of '", nm, "' must be one of: ", paste(lengths, collapse = " or "))
+      }, indots[ nms ], nms))
+  }
+
+  dots
+}
diff --git a/R/mini_plots.R b/R/mini_plots.R
index a1463f9..fc07ce5 100644
--- a/R/mini_plots.R
+++ b/R/mini_plots.R
@@ -21,7 +21,9 @@
 #' @param border Color for the border.
 #' @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. Support `png` or `svg`. SVG is recommended
+#' @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.
 #' for HTML output
 #' @param ... extra parameters sending to `hist()`
 #'
@@ -33,17 +35,17 @@
                       col = "lightgray", border = NULL,
                       dir = if (is_latex()) rmd_files_dir() else tempdir(),
                       file = NULL,
-                      file_type = if (is_latex()) "png" else "svg", ...) {
+                      file_type = if (is_latex()) "pdf" else "svg", ...) {
   if (is.list(x)) {
     if (same_lim & is.null(lim)) {
       lim <- base::range(unlist(x))
     }
-    return(lapply(x, function(x_) {spec_hist(
-      x = x_, width = width, height = height,
-      breaks = breaks, same_lim = same_lim, lim = lim,
-      xaxt = xaxt, yaxt = yaxt, ann = ann, col = col, border = border,
-      dir = dir, file = file, file_type = file_type, ...
-    )}))
+
+    dots <- listify_args(x, width, height, res, breaks,
+                         lim, xaxt, yaxt, ann, col, border,
+                         dir, file, file_type,
+                         lengths = c(1, length(x)))
+    return(do.call(Map, c(list(f = spec_hist), dots)))
   }
 
   if (is.null(x)) return(NULL)
@@ -52,42 +54,33 @@
     lim <- base::range(x)
   }
 
-  file_type <- match.arg(file_type, c("svg", "png"))
-
   if (!dir.exists(dir)) {
     dir.create(dir)
   }
 
+  file_ext <- dev_chr(file_type)
   if (is.null(file)) {
-    file <- file.path(dir, paste0(
-      "hist_", round(as.numeric(Sys.time()) * 1000), ".", file_type))
+    file <- normalizePath(
+      tempfile(pattern = "hist_", tmpdir = dir, fileext = paste0(".", file_ext)),
+      winslash = "/", mustWork = FALSE)
   }
 
-  if (file_type == "svg") {
-    grDevices::svg(filename = file, width = width / res, height = height / res,
-                   bg = 'transparent')
-  } else {
-    grDevices::png(filename = file, width = width, height = height, res = res,
-                   bg = 'transparent')
-  }
+  graphics_dev(filename = file, dev = file_type,
+               width = width, height = height, res = res,
+               bg = "transparent")
+  curdev <- grDevices::dev.cur()
+  on.exit(grDevices::dev.off(curdev), add = TRUE)
 
   graphics::par(mar = c(0, 0, 0.2, 0), lwd=0.5)
   graphics::hist(x, breaks = breaks, xlim = lim, border = border,
                  xaxt = xaxt, yaxt = yaxt, ann = ann, col = col, ...)
-  grDevices::dev.off()
 
-  if (file_type == "svg") {
-    svg_xml <- xml2::read_xml(file)
-    svg_text <- as.character(svg_xml)
-    unlink(file)
-  } else {
-    svg_text <- NULL
-  }
-  out <- list(path = file, dev = file_type, type = "hist",
-              width = width, height = height, res = res,
-              svg_text = svg_text)
+  grDevices::dev.off(curdev)
 
-  class(out) <- "kableExtraInlinePlots"
+  out <- make_inline_plot(
+    file, file_ext, file_type,
+    width, height, res,
+    del = TRUE)
   return(out)
 }
 
@@ -121,8 +114,9 @@
 #' @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. Support `png` or `svg`. SVG is recommended
-#' for HTML output
+#' @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
@@ -134,19 +128,18 @@
                          boxlty = 0, medcol = "red", medlwd = 1,
                          dir = if (is_latex()) rmd_files_dir() else tempdir(),
                          file = NULL,
-                         file_type = if (is_latex()) "png" else "svg", ...) {
+                         file_type = if (is_latex()) "pdf" else "svg", ...) {
   if (is.list(x)) {
     if (same_lim & is.null(lim)) {
       lim <- base::range(unlist(x))
     }
-    return(lapply(x, function(x_) {spec_boxplot(
-      x = x_, width = width, height = height,
-      add_label = add_label, same_lim = same_lim, lim = lim,
-      xaxt = xaxt, yaxt = yaxt, ann = ann,
-      col = col, border = border,
-      boxlty = boxlty, medcol = medcol, medlwd = medlwd,
-      dir = dir, file = file, file_type = file_type, ...
-    )}))
+
+    dots <- listify_args(x, width, height, res,
+                         add_label, label_digits,
+                         lim, xaxt, yaxt, ann, col, border,
+                         dir, file, file_type,
+                         lengths = c(1, length(x)))
+    return(do.call(Map, c(list(f = spec_boxplot), dots)))
   }
 
   if (is.null(x)) return(NULL)
@@ -157,24 +150,22 @@
     lim[2] <- (lim[2] - lim[1]) / 10 + lim[2]
   }
 
-  file_type <- match.arg(file_type, c("svg", "png"))
-
   if (!dir.exists(dir)) {
     dir.create(dir)
   }
 
+  file_ext <- dev_chr(file_type)
   if (is.null(file)) {
-    file <- file.path(dir, paste0(
-      "hist_", round(as.numeric(Sys.time()) * 1000), ".", file_type))
+    file <- normalizePath(
+      tempfile(pattern = "boxplot_", tmpdir = dir, fileext = paste0(".", file_ext)),
+      winslash = "/", mustWork = FALSE)
   }
 
-  if (file_type == "svg") {
-    grDevices::svg(filename = file, width = width / res, height = height / res,
-                   bg = 'transparent')
-  } else {
-    grDevices::png(filename = file, width = width, height = height, res = res,
-                   bg = 'transparent')
-  }
+  graphics_dev(filename = file, dev = file_type,
+               width = width, height = height, res = res,
+               bg = "transparent")
+  curdev <- grDevices::dev.cur()
+  on.exit(grDevices::dev.off(curdev), add = TRUE)
 
   graphics::par(mar = c(0, 0, 0, 0))
 
@@ -191,19 +182,13 @@
     graphics::text(x_min, y = 0.6, labels = x_min, cex = 0.5)
     graphics::text(x_max, y = 0.6, labels = x_max, cex = 0.5)
   }
-  grDevices::dev.off()
 
-  if (file_type == "svg") {
-    svg_xml <- xml2::read_xml(file)
-    svg_text <- as.character(svg_xml)
-    unlink(file)
-  } else {
-    svg_text <- NULL
-  }
-  out <- list(path = file, dev = file_type, type = "boxplot",
-              width = width, height = height, res = res,
-              svg_text = svg_text)
-  class(out) <- "kableExtraInlinePlots"
+  grDevices::dev.off(curdev)
+
+  out <- make_inline_plot(
+    file, file_ext, file_type,
+    width, height, res,
+    del = TRUE)
   return(out)
 }
 
@@ -258,8 +243,9 @@
 #' default to the value of `minmax`. Set to an empty `list()` to disable.
 #' @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. Support `png` or `svg`. SVG is recommended
-#' for HTML output.
+#' @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 ... extra parameters passing to `plot`
 #'
 #' @export
@@ -272,7 +258,7 @@
                       minmax = list(pch = ".", cex = cex, col = "red"),
                       min = minmax, max = minmax,
                       dir = if (is_latex()) rmd_files_dir() else tempdir(),
-                      file = NULL, file_type = if (is_latex()) "png" else "svg", ...) {
+                      file = NULL, file_type = if (is_latex()) "pdf" else "svg", ...) {
   if (is.list(x)) {
     lenx <- length(x)
 
@@ -291,53 +277,11 @@
       stop("'x' and 'y' are not the same length")
     }
 
-    # any of the arguments can be per-plot controlling, but an arg
-    # that is normally not length-1 may be recycled (incorrectly) by
-    # Map, so we have to listify them if not already lists;
-    # enforce a restriction of recycling only length 1 or lenx
-
-    # first, start with the literals (x,y)
-    # (same_lim is not a factor anymore)
-    dots <- list(x = x, y = y)
-
-    # second, we know these args are likely to be vectors (length > 1)
-    # or lists, so we have to handle them carefully and double-list if
-    # present
-    notlen1vec <- list(xlim = xlim, ylim = ylim)
-    dots <- c(dots, Map(
-      function(L, nm) {
-        if (is.null(L)) return(list(NULL))
-        if (!is.list(L)) return(list(L))
-        if (length(L) == lenx) return(L)
-        stop("length of '", nm, "' must be 1 or the same length as 'x'")
-      }, notlen1vec, names(notlen1vec)))
-
-    # these are special ... they are lists which may need to be
-    # nested, and we can't pass NULL since that may default to the
-    # actual values instead of the intended
-    notlen1lst <- list(minmax = minmax, min = min, max = max)
-    dots <- c(dots, Map(
-      function(L, nm) {
-        if (is.null(L)) return(list(NULL))
-        if (!length(L)) return(list(list()))
-        if (!is.list(L[[1]])) return (list(L))
-        if (length(L) == lenx) return(L)
-        stop("length of '", nm, "' must be 1 or the same length as 'x'")
-      }, notlen1lst, names(notlen1lst)))
-
-    # last, all remaining arguments that we don't already know about
-    # are length-1, so can be easily listified; using 'formals()'
-    # allows us to not hard-code all params
-    len1args <- mget(setdiff(names(formals()),
-                             c(names(dots), "same_lim", "x", "y", "...")))
-    dots <- c(dots, Map(
-      function(V, nm) {
-        if (is.null(V)) return(list(NULL))
-        if (!length(V) %in% c(1L, lenx)) {
-          stop("length of '", nm, "' must be 1 or the same length as 'x'")
-        }
-        V
-      }, len1args, names(len1args)))
+    dots <- listify_args(x, y = y, width, height, res,
+                         xlim, ylim, xaxt, yaxt, ann, col, border, frame.plot,
+                         lwd, pch, cex, type, polymin, minmax, min, max,
+                         dir, file, file_type,
+                         lengths = c(1, lenx))
 
     return(do.call(Map, c(list(f = spec_plot), dots)))
 
@@ -371,24 +315,20 @@
   xlim <- xlim + diff(xlim) * expand
   ylim <- ylim + diff(ylim) * expand
 
-  file_type <- match.arg(file_type, c("svg", "png"))
-
   if (!dir.exists(dir)) {
     dir.create(dir)
   }
 
+  file_ext <- dev_chr(file_type)
   if (is.null(file)) {
-    file <- file.path(dir, paste0(
-      "hist_", round(as.numeric(Sys.time()) * 1000), ".", file_type))
+    file <- normalizePath(
+      tempfile(pattern = "plot_", tmpdir = dir, fileext = paste0(".", file_ext)),
+      winslash = "/", mustWork = FALSE)
   }
 
-  if (file_type == "svg") {
-    grDevices::svg(filename = file, width = width / res, height = height / res,
-                   bg = 'transparent')
-  } else {
-    grDevices::png(filename = file, width = width, height = height, res = res,
-                   bg = 'transparent')
-  }
+  graphics_dev(filename = file, dev = file_type,
+               width = width, height = height, res = res,
+               bg = "transparent")
   curdev <- grDevices::dev.cur()
   on.exit(grDevices::dev.off(curdev), add = TRUE)
 
@@ -428,17 +368,9 @@
 
   grDevices::dev.off(curdev)
 
-  if (file_type == "svg") {
-    svg_xml <- xml2::read_xml(file)
-    svg_text <- as.character(svg_xml)
-    unlink(file)
-  } else {
-    svg_text <- NULL
-  }
-  out <- list(path = file, dev = file_type, type = "line",
-              width = width, height = height, res = res,
-              svg_text = svg_text)
-
-  class(out) <- "kableExtraInlinePlots"
+  out <- make_inline_plot(
+    file, file_ext, file_type,
+    width, height, res,
+    del = TRUE)
   return(out)
 }
diff --git a/man/graphics_helpers.Rd b/man/graphics_helpers.Rd
new file mode 100644
index 0000000..02210af
--- /dev/null
+++ b/man/graphics_helpers.Rd
@@ -0,0 +1,80 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/graphics_helpers.R
+\name{graphics_helpers}
+\alias{graphics_helpers}
+\alias{graphics_dev}
+\alias{is_svg}
+\alias{dev_chr}
+\title{Helper functions to use various graphics devices}
+\usage{
+graphics_dev(filename, width, height, res, ..., dev)
+
+is_svg(dev)
+
+dev_chr(dev)
+}
+\arguments{
+\item{filename}{Passed through to the graphics device.}
+
+\item{width, height}{Plot dimensions in pixels.}
+
+\item{res}{The resolution of the plot; default is 300.}
+
+\item{...}{extra parameters passing to the graphics-device function.}
+
+\item{dev}{Character (e.g., "svg", "pdf") or function (e.g.,
+\code{grDevices::svg}, \code{grDevices::pdf}).}
+}
+\value{
+'graphics_dev': nothing, a plot device is opened
+
+'is_svg': logical
+
+\code{dev_chr}: character
+}
+\description{
+These helper functions generalize the use of strings (e.g.,
+\code{"svg"}, \code{"pdf"}) or graphic device functions (e.g.,
+\code{grDevices::svg}, \code{grDevices::pdf}) for in-table plots.
+}
+\details{
+\itemize{
+\item \code{graphics_dev} generalizes the use of 'res' and plot dimensions
+across graphic devices. Raster-based devices (e.g., 'png',
+'jpeg', 'tiff', 'bmp') tend to use 'res' and the width/height
+units default to pixels. All other devices (e.g., 'pdf', 'svg')
+tend to use inches as the default units for width/height, and
+error when 'res' is provided.
+
+The current heuristic is the look for the 'res' argument in the
+function's formals; if that is present, then it is assumed that
+the default units are in pixels, so 'width', 'height', and 'res'
+are passed through unmodified. If 'res' is not present, then
+'width' and 'height' are converted from pixels to inches, and
+'res' is not passed to the function
+
+Another purpose of this function is to generalize the different
+graphic functions' use of 'file=' versus 'filename='.
+}
+
+\itemize{
+\item \code{is_svg} determines if the plot device is svg-like, typically one
+of \verb{"svg", }grDevices::svg\verb{, or }svglite::svglite`
+}
+
+\itemize{
+\item \code{dev_chr} determines the filename extension for the applicable
+plot function; when \code{dev} is a string, then it is returned
+unchanged; when \code{dev} is a function, the formals of the function
+are checked for clues (i.e., default value of a \verb{file=} argument)
+}
+}
+\section{Functions}{
+\itemize{
+\item \code{graphics_dev}: Generalize 'res' and 'filename across dev functions
+
+\item \code{is_svg}: Determine if plot device is svg-like
+
+\item \code{dev_chr}: Determine filename extension
+}}
+
diff --git a/man/listify_args.Rd b/man/listify_args.Rd
new file mode 100644
index 0000000..20f55cf
--- /dev/null
+++ b/man/listify_args.Rd
@@ -0,0 +1,52 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/graphics_helpers.R
+\name{listify_args}
+\alias{listify_args}
+\title{Convert arguments for a single call into Map-able args}
+\usage{
+listify_args(
+  ...,
+  lengths = NA,
+  passthru = c("x", "y"),
+  notlen1vec = c("lim", "xlim", "ylim"),
+  notlen1lst = c("minmax", "min", "max"),
+  ignore = c("same_lim")
+)
+}
+\arguments{
+\item{...}{Arbitrary arguments to be possibly converted into lists
+of arguments.}
+
+\item{lengths}{Allowable lengths of the arguments, typically 1 and
+the length of the main variable (e.g., "x"). If 'NA' (default),
+it is not enforced.}
+
+\item{passthru}{Character vector of variables to pass through with
+no conversion to lists of values. Extra names (not provided in
+\code{...}) are ignored.}
+
+\item{notlen1vec}{Character vector of variables that are known to
+be length over 1 for a single plot call, so it will always be
+list-ified and extra care to ensure it is grouped correctly.
+Extra names (not provided in \code{...}) are ignored.}
+
+\item{notlen1lst}{Character vector of variables that are lists, so
+the inner list length is not checked/enforced. (For example, if a
+single plot call takes an argument \code{list(a=1,b=2,d=3)} and the
+multi-data call creates three plots, then a naive match might
+think that the first plot would get \code{list(a=1)}, second plot gets
+\code{list(b=2)}, etc. Adding that list-argument to this 'notlen1lst'
+will ensure that the full list is passed correctly.) Extra names
+(not provided in \code{...}) are ignored.}
+
+\item{ignore}{Character vector of variables to ignore, never
+returned. (Generally one can control this by not adding the
+variable in the first place, but having this here allows some
+sanity checks and/or programmatic usage.)}
+}
+\value{
+list, generally a list of embedded lists
+}
+\description{
+Convert arguments for a single call into Map-able args
+}
diff --git a/man/make_inline_plot.Rd b/man/make_inline_plot.Rd
new file mode 100644
index 0000000..5312a7b
--- /dev/null
+++ b/man/make_inline_plot.Rd
@@ -0,0 +1,30 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/graphics_helpers.R
+\name{make_inline_plot}
+\alias{make_inline_plot}
+\title{Combine file (or svg text) and parameters into a 'kableExtraInlinePlots' object}
+\usage{
+make_inline_plot(filename, file_ext, dev, width, height, res, del = TRUE)
+}
+\arguments{
+\item{filename}{Passed through to the graphics device.}
+
+\item{file_ext}{Character, something like "png".}
+
+\item{dev}{Character (e.g., "svg", "pdf") or function (e.g.,}
+
+\item{width, height}{Plot dimensions in pixels.}
+
+\item{res}{The resolution of the plot; default is 300.}
+
+\item{del}{If the file is svg-like, then the default action is to
+read the file into an embedded SVG object; once done, the file is
+no longer used. The default action is to delete this file early,
+set this to 'FALSE' to keep the file.}
+}
+\value{
+list object, with class 'kableExtraInlinePlots'
+}
+\description{
+Combine file (or svg text) and parameters into a 'kableExtraInlinePlots' object
+}
diff --git a/man/spec_boxplot.Rd b/man/spec_boxplot.Rd
index ebde455..820ade8 100644
--- a/man/spec_boxplot.Rd
+++ b/man/spec_boxplot.Rd
@@ -23,7 +23,7 @@
   medlwd = 1,
   dir = if (is_latex()) rmd_files_dir() else tempdir(),
   file = NULL,
-  file_type = if (is_latex()) "png" else "svg",
+  file_type = if (is_latex()) "pdf" else "svg",
   ...
 )
 }
@@ -68,8 +68,9 @@
 
 \item{file}{File name. If not provided, a random name will be used}
 
-\item{file_type}{Graphic device. Support \code{png} or \code{svg}. SVG is recommended
-for HTML output}
+\item{file_type}{Graphic device. Can be character (e.g., \code{"pdf"})
+or a graphics device function (\code{grDevices::pdf}). This defaults
+to \code{"pdf"} if the rendering is in LaTeX and \code{"svg"} otherwise.}
 
 \item{...}{extraparameters passing to boxplot}
 }
diff --git a/man/spec_hist.Rd b/man/spec_hist.Rd
index ae97578..899dcaa 100644
--- a/man/spec_hist.Rd
+++ b/man/spec_hist.Rd
@@ -19,7 +19,7 @@
   border = NULL,
   dir = if (is_latex()) rmd_files_dir() else tempdir(),
   file = NULL,
-  file_type = if (is_latex()) "png" else "svg",
+  file_type = if (is_latex()) "pdf" else "svg",
   ...
 )
 }
@@ -52,7 +52,9 @@
 
 \item{file}{File name. If not provided, a random name will be used}
 
-\item{file_type}{Graphic device. Support \code{png} or \code{svg}. SVG is recommended
+\item{file_type}{Graphic device. Can be character (e.g., \code{"pdf"})
+or a graphics device function (\code{grDevices::pdf}). This defaults
+to \code{"pdf"} if the rendering is in LaTeX and \code{"svg"} otherwise.
 for HTML output}
 
 \item{...}{extra parameters sending to \code{hist()}}
diff --git a/man/spec_plot.Rd b/man/spec_plot.Rd
index 1917de1..829bae8 100644
--- a/man/spec_plot.Rd
+++ b/man/spec_plot.Rd
@@ -29,7 +29,7 @@
   max = minmax,
   dir = if (is_latex()) rmd_files_dir() else tempdir(),
   file = NULL,
-  file_type = if (is_latex()) "png" else "svg",
+  file_type = if (is_latex()) "pdf" else "svg",
   ...
 )
 }
@@ -85,8 +85,9 @@
 
 \item{file}{File name. If not provided, a random name will be used}
 
-\item{file_type}{Graphic device. Support \code{png} or \code{svg}. SVG is recommended
-for HTML output.}
+\item{file_type}{Graphic device. Can be character (e.g., \code{"pdf"})
+or a graphics device function (\code{grDevices::pdf}). This defaults
+to \code{"pdf"} if the rendering is in LaTeX and \code{"svg"} otherwise.}
 
 \item{...}{extra parameters passing to \code{plot}}
 }