blob: 5a5cbc08f5623fc63cd418e44db1a7b79bebfb15 [file] [log] [blame]
#' Save kable to files
#'
#' @param x A piece of HTML code for tables, usually generated by kable and
#' kableExtra
#' @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, ...) {
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),
html_dependency_kePrint()
)
html_table <- htmltools::HTML(as.character(x))
html_result <- htmltools::tagList(html_header, html_table)
# 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) {
}