improved save_kable and added webshot support
diff --git a/DESCRIPTION b/DESCRIPTION
index ca44252..201ba3d 100644
--- a/DESCRIPTION
+++ b/DESCRIPTION
@@ -43,7 +43,9 @@
grDevices,
htmltools,
rstudioapi,
- glue
+ glue,
+ tools,
+ webshot
Suggests:
testthat,
magick,
diff --git a/NAMESPACE b/NAMESPACE
index 8c5dcea..ab0fb47 100644
--- a/NAMESPACE
+++ b/NAMESPACE
@@ -68,10 +68,13 @@
importFrom(stringr,str_split)
importFrom(stringr,str_sub)
importFrom(stringr,str_trim)
+importFrom(tools,file_ext)
+importFrom(tools,file_path_sans_ext)
importFrom(utils,capture.output)
importFrom(utils,head)
importFrom(utils,read.csv)
importFrom(viridisLite,viridis)
+importFrom(webshot,webshot)
importFrom(xml2,"xml_attr<-")
importFrom(xml2,"xml_text<-")
importFrom(xml2,read_html)
diff --git a/R/kableExtra-package.R b/R/kableExtra-package.R
index 12a3579..a06fbcf 100644
--- a/R/kableExtra-package.R
+++ b/R/kableExtra-package.R
@@ -73,6 +73,8 @@
#' @importFrom grDevices col2rgb
#' @importFrom rstudioapi isAvailable viewer
#' @importFrom glue glue
+#' @importFrom tools file_ext file_path_sans_ext
+#' @importFrom webshot webshot
#' @import htmltools
#' @name kableExtra-package
#' @aliases kableExtra
diff --git a/R/save_kable.R b/R/save_kable.R
index 078842f..5a5cbc0 100644
--- a/R/save_kable.R
+++ b/R/save_kable.R
@@ -2,13 +2,24 @@
#'
#' @param x A piece of HTML code for tables, usually generated by kable and
#' kableExtra
-#' @param file save to files
+#' @param file save to files. If the input table is in HTML and the output file
+#' ends with `.png`, `.pdf` and `.jpeg`, `webshot` will be used to do the
+#' conversion
+#'
#' @param bs_theme Which Bootstrap theme to use
#' @param self_contained Will the files be self-contained?
+#' @param ... Additional variables being passed to `webshot::webshot`.`
#'
#' @export
save_kable <- function(x, file,
- bs_theme = "simplex", self_contained = TRUE) {
+ bs_theme = "simplex", self_contained = TRUE, ...) {
+ if (attr(x, "format") == "latex") {
+ return(save_kable_latex(x, file))
+ }
+ return(save_kable_html(x, file, bs_theme, self_contained, ...))
+}
+
+save_kable_html <- function(x, file, bs_theme, self_contained, ...) {
html_header <- htmltools::tags$head(
rmarkdown::html_dependency_jquery(),
rmarkdown::html_dependency_bootstrap(theme = bs_theme),
@@ -16,9 +27,23 @@
)
html_table <- htmltools::HTML(as.character(x))
html_result <- htmltools::tagList(html_header, html_table)
- htmltools::save_html(html_result, file = file)
- if (self_contained) {
- rmarkdown::pandoc_self_contained_html(file, file)
+
+ # Use webshot if necessary
+ if (tools::file_ext(file) %in% c("png", "jpg", "jpeg", "pdf")) {
+ file_html <- paste0(tools::file_path_sans_ext(file), ".html")
+ htmltools::save_html(html_result, file = file_html)
+ webshot::webshot(file_html, file, ...)
+ unlink(file_html)
unlink("lib", recursive = TRUE)
+ } else {
+ htmltools::save_html(html_result, file = file)
+ if (self_contained) {
+ rmarkdown::pandoc_self_contained_html(file, file)
+ unlink("lib", recursive = TRUE)
+ }
}
}
+
+save_kable_latex <- function(x, file) {
+
+}
diff --git a/man/save_kable.Rd b/man/save_kable.Rd
index e5bad7b..0b80071 100644
--- a/man/save_kable.Rd
+++ b/man/save_kable.Rd
@@ -4,17 +4,21 @@
\alias{save_kable}
\title{Save kable to files}
\usage{
-save_kable(x, file, bs_theme = "simplex", self_contained = TRUE)
+save_kable(x, file, bs_theme = "simplex", self_contained = TRUE, ...)
}
\arguments{
\item{x}{A piece of HTML code for tables, usually generated by kable and
kableExtra}
-\item{file}{save to files}
+\item{file}{save to files. If the input table is in HTML and the output file
+ends with \code{.png}, \code{.pdf} and \code{.jpeg}, \code{webshot} will be used to do the
+conversion}
\item{bs_theme}{Which Bootstrap theme to use}
\item{self_contained}{Will the files be self-contained?}
+
+\item{...}{Additional variables being passed to \code{webshot::webshot}.`}
}
\description{
Save kable to files