Add as_image; Add xcolor back in but without options
diff --git a/R/as_image.R b/R/as_image.R
new file mode 100644
index 0000000..245ecb1
--- /dev/null
+++ b/R/as_image.R
@@ -0,0 +1,42 @@
+#' Render the table as an format-independent image and use it in rmarkdown
+#'
+#' @description This function generates a temporary png file using `save_kable`
+#' and then try to put it in an rmarkdown document using
+#' `knitr::include_graphics`.
+#'
+#' @param x kable input. Either HTML or LaTeX
+#' @param width Image width in inches. (1 inch = 2.54 cm)
+#' @param height Image height in inches. (1 inch = 2.54 cm)
+#' @param ... Additional arguments passed to save_kable.
+#'
+#'
+#' @export
+as_image <- function(x, width = NULL, height = NULL,
+ ...) {
+ if (is.null(width) + is.null(height) == 0) {
+ message("Both width and height were defined. Use width only by default. ")
+ height <- NULL
+ }
+
+ temp_png <- tempfile(fileext = ".png")
+ temp_img <- save_kable(x = x, file = temp_png, ...)
+
+ img_dpi <- 300
+
+ if (is.null(width) + is.null(height) <= 1 & is.null(attr(temp_img, "info"))) {
+ warning("You need to install magick in order to use width/height in ",
+ "as_image. ")
+ } else {
+ if (!is.null(width)) {
+ img_dpi <- attr(temp_img, "info")$width / width
+ }
+ if (!is.null(height)) {
+ img_dpi <- attr(temp_img, "info")$height / height
+ }
+ }
+
+ include_graphics(temp_png, dpi = img_dpi)
+ return(NULL)
+}
+
+
diff --git a/R/kable_as_image.R b/R/kable_as_image.R
index 46dbcbc..4876db0 100644
--- a/R/kable_as_image.R
+++ b/R/kable_as_image.R
@@ -45,6 +45,8 @@
keep_pdf = FALSE,
density = 300,
keep_tex = FALSE) {
+ message('kable_as_image is deprecated. Please use save_kable or as_image ',
+ 'instead.')
if (!requireNamespace("magick", quietly = TRUE)) {
stop('kable_as_image requires the magick package, which is not available ',
'on all platforms. Please get it installed ',
diff --git a/R/save_kable.R b/R/save_kable.R
index 7870af7..6edc7c4 100644
--- a/R/save_kable.R
+++ b/R/save_kable.R
@@ -49,18 +49,30 @@
# Use webshot if necessary
if (tools::file_ext(file) %in% c("png", "jpg", "jpeg", "pdf")) {
- message("Putting together a HTML file...")
file_html <- paste0(tools::file_path_sans_ext(file), ".html")
+ file.create(file_html)
+ file_html <- normalizePath(file_html)
+ file <- paste0(tools::file_path_sans_ext(file_html), ".",
+ tools::file_ext(file))
htmltools::save_html(html_result, file = file_html)
- message("Converting HTML to ", tools::file_ext(file), "...")
webshot::webshot(file_html, file, ...)
- message("Done. ")
if (tools::file_ext(file) == "pdf") {
message("Note that HTML color may not be displayed on PDF properly.")
}
unlink(file_html)
- unlink("lib", recursive = TRUE)
+ unlink(file.path(dirname(file_html), "lib"), recursive = TRUE)
+ if (requireNamespace("magick", quietly = TRUE)) {
+ img_rework <- magick::image_read(file)
+ img_rework <- magick::image_trim(img_rework)
+ img_info <- magick::image_info(img_rework)
+ magick::image_write(img_rework, file)
+ attr(file, "info") <- img_info
+ } else {
+ message("save_kable will have the best result with magick installed. ")
+ }
} else {
+ file.create(file)
+ file <- normalizePath(file)
htmltools::save_html(html_result, file = file)
if (self_contained) {
rmarkdown::pandoc_self_contained_html(file, file)
@@ -68,7 +80,7 @@
}
}
- return(file)
+ return(invisible(file))
}
save_kable_latex <- function(x, file, latex_header_includes, keep_tex) {
@@ -94,16 +106,21 @@
temp_tex_file <- paste0(tools::file_path_sans_ext(file), ".tex")
writeLines(temp_tex, temp_tex_file, useBytes = T)
- system(paste0("xelatex -interaction=batchmode ", temp_tex_file))
+ temp_tex_file <- normalizePath(temp_tex_file)
+ file_no_ext <- tools::file_path_sans_ext(temp_tex_file)
+
+ owd <- setwd(dirname(temp_tex_file))
+
+ system(paste0("xelatex ", temp_tex_file))
if (!keep_tex) {
- temp_file_delete <- paste0(tools::file_path_sans_ext(file),
- c(".tex", ".aux", ".log"))
+ temp_file_delete <- paste0(file_no_ext, c(".tex", ".aux", ".log"))
unlink(temp_file_delete)
}
+ table_img_info <- NULL
if (tools::file_ext(file) != "pdf") {
table_img_pdf <- try(
- magick::image_read(paste0(tools::file_path_sans_ext(file), ".pdf"),
+ magick::image_read(paste0(file_no_ext, ".pdf"),
density = 300), silent = T)
if (class(table_img_pdf) == "try-error") {
stop("We hit an error when trying to use magick to read the generated ",
@@ -112,11 +129,17 @@
"Otherwise, you may check your magick installation and try to ",
"use magick::image_read to read the PDF file manually. ")
}
- unlink(paste0(tools::file_path_sans_ext(file), ".pdf"))
+ unlink(paste0(file_no_ext, ".pdf"))
table_img <- magick::image_convert(table_img_pdf,
tools::file_ext(file))
- magick::image_write(table_img, file)
+ table_img_info <- magick::image_info(table_img)
+ magick::image_write(table_img,
+ paste0(file_no_ext, ".", tools::file_ext(file)))
}
- return(file)
+ setwd(owd)
+
+ out <- paste0(file_no_ext, ".", tools::file_ext(file))
+ attr(out, "info") <- table_img_info
+ return(invisible(out))
}
diff --git a/R/util.R b/R/util.R
index 23a4802..2d91900 100644
--- a/R/util.R
+++ b/R/util.R
@@ -114,7 +114,8 @@
"\\usepackage[normalem]{ulem}",
"\\usepackage[normalem]{ulem}",
"\\usepackage[utf8]{inputenc}",
- "\\usepackage{makecell}"
+ "\\usepackage{makecell}",
+ "\\usepackage{xcolor}"
))
}
diff --git a/R/zzz.R b/R/zzz.R
index 2aa54f0..2014072 100644
--- a/R/zzz.R
+++ b/R/zzz.R
@@ -14,6 +14,7 @@
usepackage_latex("threeparttablex")
usepackage_latex("ulem", "normalem")
usepackage_latex("makecell")
+ usepackage_latex("xcolor")
}
auto_format <- getOption("kableExtra.auto_format", default = TRUE)
if (auto_format) auto_set_format()