fix bugs before cran release
diff --git a/R/graphics_helpers.R b/R/graphics_helpers.R
index 993a9c3..b74d439 100644
--- a/R/graphics_helpers.R
+++ b/R/graphics_helpers.R
@@ -183,7 +183,7 @@
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)
+ if ((length(lengths) == 1 && is.na(lengths)) || length(L) %in% lengths) return(L)
stop("length of '", nm, "' must be one of: ", paste(lengths, collapse = " or "))
}, indots[ nms ], nms))
}
@@ -197,7 +197,7 @@
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)
+ if ((length(lengths) == 1 && is.na(lengths)) || length(L) %in% lengths) return(L)
stop("length of '", nm, "' must be one of: ", paste(lengths, collapse = " or "))
}, indots[ nms ], nms))
}
@@ -210,7 +210,7 @@
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)
+ if ((length(lengths) == 1 && is.na(lengths)) || length(V) %in% lengths) return(V)
stop("length of '", nm, "' must be one of: ", paste(lengths, collapse = " or "))
}, indots[ nms ], nms))
}
diff --git a/R/kableExtra-package.R b/R/kableExtra-package.R
index 799dac1..48543e2 100644
--- a/R/kableExtra-package.R
+++ b/R/kableExtra-package.R
@@ -68,7 +68,7 @@
#' @importFrom utils read.csv head capture.output
#' @importFrom scales rescale
#' @importFrom viridisLite viridis
-#' @importFrom stats ave density median
+#' @importFrom stats ave density median na.omit setNames
#' @importFrom grDevices col2rgb svg png
#' @importFrom rstudioapi isAvailable viewer
#' @importFrom glue glue
diff --git a/R/mini_plots.R b/R/mini_plots.R
index fc8e031..cef8c5e 100644
--- a/R/mini_plots.R
+++ b/R/mini_plots.R
@@ -2,14 +2,17 @@
#'
#' @description These functions helps you quickly generate sets of sparkline
#' style plots using base R plotting system. Currently, we support histogram,
-#' boxplot, and line. 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.
+#' boxplot, line, scatter and pointrange 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 breaks The `break` option in `hist`. Default is "Sturges" but you can
+#' also provide a vector to manually specify break points.
#' @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
@@ -88,9 +91,10 @@
#'
#' @description These functions helps you quickly generate sets of sparkline
#' style plots using base R plotting system. Currently, we support histogram,
-#' boxplot, and line. 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.
+#' boxplot, line, scatter and pointrange 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
@@ -101,9 +105,8 @@
#' 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,xlim,ylim Manually specify plotting range in the form of
-#' `c(0, 10)`. `lim` is used in `spec_hist` and `spec_boxplot`; `xlim`
-#' and `ylim` are used in `spec_plot`.
+#' @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)
@@ -170,10 +173,10 @@
graphics::par(mar = c(0, 0, 0, 0))
graphics::boxplot(x, horizontal = TRUE, ann = ann, frame = FALSE, bty = 'n', ylim = lim,
- col = col, border = border,
- boxlty = boxlty, medcol = medcol, medlwd = medlwd,
- axes = FALSE, outcex = 0.2, whisklty = 1,
- ...)
+ col = col, border = border,
+ boxlty = boxlty, medcol = medcol, medlwd = medlwd,
+ axes = FALSE, outcex = 0.2, whisklty = 1,
+ ...)
if (add_label) {
x_median <- round(median(x, na.rm = T), label_digits)
x_min <- round(min(x, na.rm = T), label_digits)
@@ -207,9 +210,10 @@
#'
#' @description These functions helps you quickly generate sets of sparkline
#' style plots using base R plotting system. Currently, we support histogram,
-#' boxplot, and line. 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.
+#' boxplot, line, scatter and pointrange 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,y Vector of values or List of vectors of values. y is optional.
#' @param width The width of the plot in pixel
@@ -349,7 +353,7 @@
if (!is.na(polymin)) {
lty <- if ("lty" %in% names(dots)) dots$lty else graphics::par("lty")
- polygon(c(x[1], x, x[length(x)]), c(polymin, y, polymin),
+ graphics::polygon(c(x[1], x, x[length(x)]), c(polymin, y, polymin),
border = NA, col = col, angle = angle, lty = lty,
xpd = if ("xpd" %in% names(dots)) dots$xpd else NA)
}
@@ -374,3 +378,109 @@
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 and pointrange 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,xmin,xmax A scalar value or List of scalar values for dot, left
+#' and right errorbar.
+#' @param vline A scalar value for where to draw a vertical line.
+#' @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 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 cex size of the mean dot and error bar size.
+#' @param frame.plot T/F for whether to plot the plot frames.
+#' @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.
+#' for HTML output
+#' @param ... extra parameters sending to `hist()`
+#'
+#' @export
+spec_pointrange <- function(
+ x, xmin, xmax, vline = NULL,
+ width = 200, height = 50, res = 300,
+ same_lim = TRUE, lim = NULL,
+ xaxt = 'n', yaxt = 'n', ann = FALSE,
+ col = "red", cex = 0.3, frame.plot = FALSE,
+ dir = if (is_latex()) rmd_files_dir() else tempdir(),
+ file = NULL,
+ file_type = if (is_latex()) "pdf" else "svg", ...) {
+ if (length(x) > 1) {
+ if (same_lim & is.null(lim)) {
+ all_range <- c(unlist(xmin), unlist(xmax))
+ lim <- base::range(all_range, na.rm=TRUE)
+ lim <- lim + c(-0.04 * diff(lim), 0.04 * diff(lim))
+ }
+
+ dots <- listify_args(
+ x = as.list(x), xmin = as.list(xmin), xmax = as.list(xmax), vline,
+ width, height, res,
+ lim, xaxt, yaxt, ann, col, cex, frame.plot,
+ dir, file, file_type,
+ lengths = c(1, length(x)),
+ passthru = c("x", "xmin", "xmax"))
+ return(do.call(Map, c(list(f = spec_pointrange), dots)))
+ }
+
+ if (is.null(x)) return(NULL)
+
+ if (is.null(lim)) {
+ one_range <- unlist(c(xmin, xmax))
+ lim <- base::range(one_range, na.rm=TRUE)
+ lim <- lim + c(-0.04 * diff(lim), 0.04 * diff(lim))
+ }
+
+ if (!dir.exists(dir)) {
+ dir.create(dir)
+ }
+
+ file_ext <- dev_chr(file_type)
+ if (is.null(file)) {
+ file <- normalizePath(
+ tempfile(pattern = "pointrange_", tmpdir = dir, fileext = paste0(".", file_ext)),
+ winslash = "/", mustWork = FALSE)
+ }
+
+ 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=1,
+ ann = ann, xaxt = xaxt, yaxt = yaxt)
+
+ graphics::plot(x, 0, type = "p", pch = ".",
+ xlim = lim, frame.plot = frame.plot)
+ graphics::arrows(xmin, 0, xmax, 0, cex / 15, angle = 90, code = 3)
+ graphics::points(x, 0, col = col, type = "p", pch = 15, cex = cex)
+ if (!is.null(vline)) {
+ graphics::abline(v = vline, lty = 3)
+ }
+
+ grDevices::dev.off(curdev)
+
+ out <- make_inline_plot(
+ file, file_ext, file_type,
+ width, height, res,
+ del = TRUE)
+ return(out)
+}
diff --git a/R/save_kable.R b/R/save_kable.R
index fe80d8f..c8b7185 100644
--- a/R/save_kable.R
+++ b/R/save_kable.R
@@ -188,7 +188,7 @@
output <- normalizePath(output)
template <- tempfile(fileext = ".html")
on.exit(unlink(template), add = TRUE)
- rmarkdown:::write_utf8("$body$", template)
+ write_utf8("$body$", template)
from <- if (rmarkdown::pandoc_available("1.17")) "markdown_strict" else "markdown"
rmarkdown::pandoc_convert(
input = input, from = from, output = output,
@@ -197,6 +197,13 @@
invisible(output)
}
+# Local version of rmarkdown:::write_utf8
+write_utf8 <- function (text, con, ...) {
+ opts <- options(encoding = "native.enc")
+ on.exit(options(opts), add = TRUE)
+ writeLines(enc2utf8(text), con, ..., useBytes = TRUE)
+}
+
remove_html_doc <- function(x){