Hao Zhu | 73cf373 | 2018-05-11 17:50:05 -0400 | [diff] [blame] | 1 | #' Save kable to files |
| 2 | #' |
| 3 | #' @param x A piece of HTML code for tables, usually generated by kable and |
| 4 | #' kableExtra |
Hao Zhu | 7f8b684 | 2018-10-23 17:41:13 -0400 | [diff] [blame] | 5 | #' @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 Zhu | 7039ecf | 2019-01-06 17:51:21 -0500 | [diff] [blame] | 7 | #' conversion. |
Hao Zhu | 73cf373 | 2018-05-11 17:50:05 -0400 | [diff] [blame] | 8 | #' @param bs_theme Which Bootstrap theme to use |
| 9 | #' @param self_contained Will the files be self-contained? |
Hao Zhu | 7039ecf | 2019-01-06 17:51:21 -0500 | [diff] [blame] | 10 | #' @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 Zhu | 73cf373 | 2018-05-11 17:50:05 -0400 | [diff] [blame] | 21 | #' |
| 22 | #' @export |
| 23 | save_kable <- function(x, file, |
Hao Zhu | 7039ecf | 2019-01-06 17:51:21 -0500 | [diff] [blame] | 24 | bs_theme = "simplex", self_contained = TRUE, |
| 25 | extra_dependencies = NULL, ..., |
| 26 | latex_header_includes = NULL, keep_tex = FALSE) { |
Hao Zhu | 7f8b684 | 2018-10-23 17:41:13 -0400 | [diff] [blame] | 27 | if (attr(x, "format") == "latex") { |
Hao Zhu | 7039ecf | 2019-01-06 17:51:21 -0500 | [diff] [blame] | 28 | return(save_kable_latex(x, file, latex_header_includes, keep_tex)) |
Hao Zhu | 7f8b684 | 2018-10-23 17:41:13 -0400 | [diff] [blame] | 29 | } |
Hao Zhu | 7039ecf | 2019-01-06 17:51:21 -0500 | [diff] [blame] | 30 | return(save_kable_html(x, file, bs_theme, self_contained, |
| 31 | extra_dependencies, ...)) |
Hao Zhu | 7f8b684 | 2018-10-23 17:41:13 -0400 | [diff] [blame] | 32 | } |
| 33 | |
Hao Zhu | 7039ecf | 2019-01-06 17:51:21 -0500 | [diff] [blame] | 34 | save_kable_html <- function(x, file, bs_theme, self_contained, |
| 35 | extra_dependencies, ...) { |
| 36 | dependencies <- list( |
Hao Zhu | 73cf373 | 2018-05-11 17:50:05 -0400 | [diff] [blame] | 37 | rmarkdown::html_dependency_jquery(), |
| 38 | rmarkdown::html_dependency_bootstrap(theme = bs_theme), |
| 39 | html_dependency_kePrint() |
| 40 | ) |
Hao Zhu | 7039ecf | 2019-01-06 17:51:21 -0500 | [diff] [blame] | 41 | if (!is.null(extra_dependencies)) { |
| 42 | dependencies <- append(dependencies, extra_dependencies) |
| 43 | } |
| 44 | |
| 45 | html_header <- htmltools::tag("head", dependencies) |
Hao Zhu | 73cf373 | 2018-05-11 17:50:05 -0400 | [diff] [blame] | 46 | html_table <- htmltools::HTML(as.character(x)) |
| 47 | html_result <- htmltools::tagList(html_header, html_table) |
Hao Zhu | 7f8b684 | 2018-10-23 17:41:13 -0400 | [diff] [blame] | 48 | |
| 49 | # Use webshot if necessary |
| 50 | if (tools::file_ext(file) %in% c("png", "jpg", "jpeg", "pdf")) { |
Hao Zhu | 7039ecf | 2019-01-06 17:51:21 -0500 | [diff] [blame] | 51 | message("Putting together a HTML file...") |
Hao Zhu | 7f8b684 | 2018-10-23 17:41:13 -0400 | [diff] [blame] | 52 | file_html <- paste0(tools::file_path_sans_ext(file), ".html") |
| 53 | htmltools::save_html(html_result, file = file_html) |
Hao Zhu | 7039ecf | 2019-01-06 17:51:21 -0500 | [diff] [blame] | 54 | message("Converting HTML to ", tools::file_ext(file), "...") |
Hao Zhu | 7f8b684 | 2018-10-23 17:41:13 -0400 | [diff] [blame] | 55 | webshot::webshot(file_html, file, ...) |
Hao Zhu | 7039ecf | 2019-01-06 17:51:21 -0500 | [diff] [blame] | 56 | message("Done. ") |
| 57 | if (tools::file_ext(file) == "pdf") { |
| 58 | message("Note that HTML color may not be displayed on PDF properly.") |
| 59 | } |
Hao Zhu | 7f8b684 | 2018-10-23 17:41:13 -0400 | [diff] [blame] | 60 | unlink(file_html) |
Hao Zhu | 73cf373 | 2018-05-11 17:50:05 -0400 | [diff] [blame] | 61 | unlink("lib", recursive = TRUE) |
Hao Zhu | 7f8b684 | 2018-10-23 17:41:13 -0400 | [diff] [blame] | 62 | } else { |
| 63 | htmltools::save_html(html_result, file = file) |
| 64 | if (self_contained) { |
| 65 | rmarkdown::pandoc_self_contained_html(file, file) |
| 66 | unlink("lib", recursive = TRUE) |
| 67 | } |
Hao Zhu | 73cf373 | 2018-05-11 17:50:05 -0400 | [diff] [blame] | 68 | } |
| 69 | } |
Hao Zhu | 7f8b684 | 2018-10-23 17:41:13 -0400 | [diff] [blame] | 70 | |
Hao Zhu | 7039ecf | 2019-01-06 17:51:21 -0500 | [diff] [blame] | 71 | save_kable_latex <- function(x, file, latex_header_includes, keep_tex) { |
| 72 | temp_tex <- c( |
| 73 | "\\documentclass[border=1mm, preview]{standalone}", |
| 74 | "\\usepackage[active,tightpage]{preview}", |
| 75 | "\\usepackage{varwidth}", |
| 76 | "\\usepackage{amssymb, amsmath}", |
| 77 | "\\usepackage{ifxetex,ifluatex}", |
| 78 | "\\usepackage{fixltx2e}", |
| 79 | "\\usepackage{polyglossia}", |
| 80 | "\\setmainlanguage{$mainlang$}", |
| 81 | latex_pkg_list(), |
| 82 | "\\usepackage{graphicx}", |
| 83 | "\\usepackage{mathspec}", |
| 84 | "\\usepackage{xltxtra,xunicode}", |
| 85 | latex_header_includes, |
| 86 | "\\begin{document}", |
| 87 | solve_enc(x), |
| 88 | "\\end{document}" |
| 89 | ) |
| 90 | temp_tex <- paste(temp_tex, collapse = "\n") |
Hao Zhu | 7f8b684 | 2018-10-23 17:41:13 -0400 | [diff] [blame] | 91 | |
Hao Zhu | 7039ecf | 2019-01-06 17:51:21 -0500 | [diff] [blame] | 92 | temp_tex_file <- tools::file_path_sans_ext(file) |
| 93 | writeLines(temp_tex, temp_tex_file, useBytes = T) |
| 94 | system(paste0("xelatex -interaction=batchmode ", temp_tex_file)) |
| 95 | if (!keep_tex) { |
| 96 | temp_file_delete <- paste0(tools::file_path_sans_ext(file), |
| 97 | c(".tex", ".aux", ".log")) |
| 98 | unlink(temp_file_delete) |
| 99 | } |
| 100 | |
| 101 | table_img_pdf <- try(magick::image_read(paste0(temp_file, ".pdf"), |
| 102 | density = density), |
| 103 | silent = T) |
| 104 | if (class(table_img_pdf) == "try-error") { |
| 105 | stop("Ghostscript is required to read PDF on windows. ", |
| 106 | "Please download it here: https://ghostscript.com/") |
| 107 | } |
| 108 | if (!keep_pdf) { |
| 109 | unlink(paste0(temp_file, ".pdf")) |
| 110 | } |
| 111 | table_img <- magick::image_convert(table_img_pdf, file_format) |
| 112 | if (!is.null(filename)) { |
| 113 | temp_img <- paste0(filename, ".", file_format) |
| 114 | } else { |
| 115 | temp_img <- tempfile(fileext = paste0(".", file_format)) |
| 116 | } |
| 117 | magick::image_write(table_img, temp_img) |
| 118 | |
| 119 | include_graphics(temp_img) |
Hao Zhu | 7f8b684 | 2018-10-23 17:41:13 -0400 | [diff] [blame] | 120 | } |