blob: 7870af76fce312dcc2a15d08fbe1f2628aadae48 [file] [log] [blame]
Hao Zhu73cf3732018-05-11 17:50:05 -04001#' Save kable to files
2#'
3#' @param x A piece of HTML code for tables, usually generated by kable and
4#' kableExtra
Hao Zhu7f8b6842018-10-23 17:41:13 -04005#' @param file save to files. If the input table is in HTML and the output file
6#' ends with `.png`, `.pdf` and `.jpeg`, `webshot` will be used to do the
Hao Zhu7039ecf2019-01-06 17:51:21 -05007#' conversion.
Hao Zhu73cf3732018-05-11 17:50:05 -04008#' @param bs_theme Which Bootstrap theme to use
9#' @param self_contained Will the files be self-contained?
Hao Zhu7039ecf2019-01-06 17:51:21 -050010#' @param extra_dependencies Additional HTML dependencies. For example,
11#' `list(`
12#' @param ... Additional variables being passed to `webshot::webshot`. This
13#' is for HTML only.
14#' @param latex_header_includes A character vector of extra LaTeX header stuff.
15#' Each element is a row. You can have things like
16#' `c("\\\\usepackage{threeparttable}", "\\\\usepackage{icons}")` You could
17#' probably add your language package here if you use non-English text in your
18#' table, such as `\\\\usepackage[magyar]{babel}`.
19#' @param keep_tex A T/F option to control if the latex file that is initially created
20#' should be kept. Default is `FALSE`.
Hao Zhu73cf3732018-05-11 17:50:05 -040021#'
22#' @export
23save_kable <- function(x, file,
Hao Zhu7039ecf2019-01-06 17:51:21 -050024 bs_theme = "simplex", self_contained = TRUE,
25 extra_dependencies = NULL, ...,
26 latex_header_includes = NULL, keep_tex = FALSE) {
Hao Zhud8516932019-01-06 18:56:47 -050027 if (!is.null(attr(x, "format")) && attr(x, "format") == "latex") {
Hao Zhu7039ecf2019-01-06 17:51:21 -050028 return(save_kable_latex(x, file, latex_header_includes, keep_tex))
Hao Zhu7f8b6842018-10-23 17:41:13 -040029 }
Hao Zhu7039ecf2019-01-06 17:51:21 -050030 return(save_kable_html(x, file, bs_theme, self_contained,
31 extra_dependencies, ...))
Hao Zhu7f8b6842018-10-23 17:41:13 -040032}
33
Hao Zhu7039ecf2019-01-06 17:51:21 -050034save_kable_html <- function(x, file, bs_theme, self_contained,
35 extra_dependencies, ...) {
36 dependencies <- list(
Hao Zhu73cf3732018-05-11 17:50:05 -040037 rmarkdown::html_dependency_jquery(),
38 rmarkdown::html_dependency_bootstrap(theme = bs_theme),
Hao Zhud8516932019-01-06 18:56:47 -050039 rmarkdown::html_dependency_font_awesome(),
Hao Zhu73cf3732018-05-11 17:50:05 -040040 html_dependency_kePrint()
41 )
Hao Zhu7039ecf2019-01-06 17:51:21 -050042 if (!is.null(extra_dependencies)) {
43 dependencies <- append(dependencies, extra_dependencies)
44 }
45
46 html_header <- htmltools::tag("head", dependencies)
Hao Zhu73cf3732018-05-11 17:50:05 -040047 html_table <- htmltools::HTML(as.character(x))
48 html_result <- htmltools::tagList(html_header, html_table)
Hao Zhu7f8b6842018-10-23 17:41:13 -040049
50 # Use webshot if necessary
51 if (tools::file_ext(file) %in% c("png", "jpg", "jpeg", "pdf")) {
Hao Zhu7039ecf2019-01-06 17:51:21 -050052 message("Putting together a HTML file...")
Hao Zhu7f8b6842018-10-23 17:41:13 -040053 file_html <- paste0(tools::file_path_sans_ext(file), ".html")
54 htmltools::save_html(html_result, file = file_html)
Hao Zhu7039ecf2019-01-06 17:51:21 -050055 message("Converting HTML to ", tools::file_ext(file), "...")
Hao Zhu7f8b6842018-10-23 17:41:13 -040056 webshot::webshot(file_html, file, ...)
Hao Zhu7039ecf2019-01-06 17:51:21 -050057 message("Done. ")
58 if (tools::file_ext(file) == "pdf") {
59 message("Note that HTML color may not be displayed on PDF properly.")
60 }
Hao Zhu7f8b6842018-10-23 17:41:13 -040061 unlink(file_html)
Hao Zhu73cf3732018-05-11 17:50:05 -040062 unlink("lib", recursive = TRUE)
Hao Zhu7f8b6842018-10-23 17:41:13 -040063 } else {
64 htmltools::save_html(html_result, file = file)
65 if (self_contained) {
66 rmarkdown::pandoc_self_contained_html(file, file)
67 unlink("lib", recursive = TRUE)
68 }
Hao Zhu73cf3732018-05-11 17:50:05 -040069 }
Hao Zhud8516932019-01-06 18:56:47 -050070
71 return(file)
Hao Zhu73cf3732018-05-11 17:50:05 -040072}
Hao Zhu7f8b6842018-10-23 17:41:13 -040073
Hao Zhu7039ecf2019-01-06 17:51:21 -050074save_kable_latex <- function(x, file, latex_header_includes, keep_tex) {
75 temp_tex <- c(
76 "\\documentclass[border=1mm, preview]{standalone}",
77 "\\usepackage[active,tightpage]{preview}",
78 "\\usepackage{varwidth}",
79 "\\usepackage{amssymb, amsmath}",
80 "\\usepackage{ifxetex,ifluatex}",
81 "\\usepackage{fixltx2e}",
82 "\\usepackage{polyglossia}",
83 "\\setmainlanguage{$mainlang$}",
84 latex_pkg_list(),
85 "\\usepackage{graphicx}",
86 "\\usepackage{mathspec}",
87 "\\usepackage{xltxtra,xunicode}",
88 latex_header_includes,
89 "\\begin{document}",
90 solve_enc(x),
91 "\\end{document}"
92 )
93 temp_tex <- paste(temp_tex, collapse = "\n")
Hao Zhu7f8b6842018-10-23 17:41:13 -040094
Hao Zhud8516932019-01-06 18:56:47 -050095 temp_tex_file <- paste0(tools::file_path_sans_ext(file), ".tex")
Hao Zhu7039ecf2019-01-06 17:51:21 -050096 writeLines(temp_tex, temp_tex_file, useBytes = T)
97 system(paste0("xelatex -interaction=batchmode ", temp_tex_file))
98 if (!keep_tex) {
99 temp_file_delete <- paste0(tools::file_path_sans_ext(file),
100 c(".tex", ".aux", ".log"))
101 unlink(temp_file_delete)
102 }
103
Hao Zhud8516932019-01-06 18:56:47 -0500104 if (tools::file_ext(file) != "pdf") {
105 table_img_pdf <- try(
106 magick::image_read(paste0(tools::file_path_sans_ext(file), ".pdf"),
107 density = 300), silent = T)
108 if (class(table_img_pdf) == "try-error") {
109 stop("We hit an error when trying to use magick to read the generated ",
110 "PDF file. If you are using Windows, it could be possible that you",
111 " had not installed ghostscript (https://ghostscript.com/). ",
112 "Otherwise, you may check your magick installation and try to ",
113 "use magick::image_read to read the PDF file manually. ")
114 }
115 unlink(paste0(tools::file_path_sans_ext(file), ".pdf"))
116 table_img <- magick::image_convert(table_img_pdf,
117 tools::file_ext(file))
118 magick::image_write(table_img, file)
Hao Zhu7039ecf2019-01-06 17:51:21 -0500119 }
Hao Zhu7039ecf2019-01-06 17:51:21 -0500120
Hao Zhud8516932019-01-06 18:56:47 -0500121 return(file)
Hao Zhu7f8b6842018-10-23 17:41:13 -0400122}