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 | d851693 | 2019-01-06 18:56:47 -0500 | [diff] [blame] | 27 | if (!is.null(attr(x, "format")) && 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), |
Hao Zhu | d851693 | 2019-01-06 18:56:47 -0500 | [diff] [blame] | 39 | rmarkdown::html_dependency_font_awesome(), |
Hao Zhu | 73cf373 | 2018-05-11 17:50:05 -0400 | [diff] [blame] | 40 | html_dependency_kePrint() |
| 41 | ) |
Hao Zhu | 7039ecf | 2019-01-06 17:51:21 -0500 | [diff] [blame] | 42 | if (!is.null(extra_dependencies)) { |
| 43 | dependencies <- append(dependencies, extra_dependencies) |
| 44 | } |
| 45 | |
| 46 | html_header <- htmltools::tag("head", dependencies) |
Hao Zhu | 73cf373 | 2018-05-11 17:50:05 -0400 | [diff] [blame] | 47 | html_table <- htmltools::HTML(as.character(x)) |
| 48 | html_result <- htmltools::tagList(html_header, html_table) |
Hao Zhu | 7f8b684 | 2018-10-23 17:41:13 -0400 | [diff] [blame] | 49 | |
| 50 | # Use webshot if necessary |
| 51 | if (tools::file_ext(file) %in% c("png", "jpg", "jpeg", "pdf")) { |
| 52 | file_html <- paste0(tools::file_path_sans_ext(file), ".html") |
Hao Zhu | f1873a4 | 2019-01-07 15:57:01 -0500 | [diff] [blame] | 53 | file.create(file_html) |
| 54 | file_html <- normalizePath(file_html) |
| 55 | file <- paste0(tools::file_path_sans_ext(file_html), ".", |
| 56 | tools::file_ext(file)) |
Hao Zhu | 7f8b684 | 2018-10-23 17:41:13 -0400 | [diff] [blame] | 57 | htmltools::save_html(html_result, file = file_html) |
| 58 | webshot::webshot(file_html, file, ...) |
Hao Zhu | 7039ecf | 2019-01-06 17:51:21 -0500 | [diff] [blame] | 59 | if (tools::file_ext(file) == "pdf") { |
| 60 | message("Note that HTML color may not be displayed on PDF properly.") |
| 61 | } |
Hao Zhu | 7f8b684 | 2018-10-23 17:41:13 -0400 | [diff] [blame] | 62 | unlink(file_html) |
Hao Zhu | f1873a4 | 2019-01-07 15:57:01 -0500 | [diff] [blame] | 63 | unlink(file.path(dirname(file_html), "lib"), recursive = TRUE) |
| 64 | if (requireNamespace("magick", quietly = TRUE)) { |
| 65 | img_rework <- magick::image_read(file) |
| 66 | img_rework <- magick::image_trim(img_rework) |
| 67 | img_info <- magick::image_info(img_rework) |
| 68 | magick::image_write(img_rework, file) |
| 69 | attr(file, "info") <- img_info |
| 70 | } else { |
| 71 | message("save_kable will have the best result with magick installed. ") |
| 72 | } |
Hao Zhu | 7f8b684 | 2018-10-23 17:41:13 -0400 | [diff] [blame] | 73 | } else { |
Hao Zhu | f1873a4 | 2019-01-07 15:57:01 -0500 | [diff] [blame] | 74 | file.create(file) |
| 75 | file <- normalizePath(file) |
Hao Zhu | 7f8b684 | 2018-10-23 17:41:13 -0400 | [diff] [blame] | 76 | htmltools::save_html(html_result, file = file) |
| 77 | if (self_contained) { |
| 78 | rmarkdown::pandoc_self_contained_html(file, file) |
| 79 | unlink("lib", recursive = TRUE) |
| 80 | } |
Hao Zhu | 73cf373 | 2018-05-11 17:50:05 -0400 | [diff] [blame] | 81 | } |
Hao Zhu | d851693 | 2019-01-06 18:56:47 -0500 | [diff] [blame] | 82 | |
Hao Zhu | f1873a4 | 2019-01-07 15:57:01 -0500 | [diff] [blame] | 83 | return(invisible(file)) |
Hao Zhu | 73cf373 | 2018-05-11 17:50:05 -0400 | [diff] [blame] | 84 | } |
Hao Zhu | 7f8b684 | 2018-10-23 17:41:13 -0400 | [diff] [blame] | 85 | |
Hao Zhu | 7039ecf | 2019-01-06 17:51:21 -0500 | [diff] [blame] | 86 | save_kable_latex <- function(x, file, latex_header_includes, keep_tex) { |
| 87 | temp_tex <- c( |
| 88 | "\\documentclass[border=1mm, preview]{standalone}", |
| 89 | "\\usepackage[active,tightpage]{preview}", |
| 90 | "\\usepackage{varwidth}", |
| 91 | "\\usepackage{amssymb, amsmath}", |
| 92 | "\\usepackage{ifxetex,ifluatex}", |
| 93 | "\\usepackage{fixltx2e}", |
| 94 | "\\usepackage{polyglossia}", |
Hao Zhu | 7039ecf | 2019-01-06 17:51:21 -0500 | [diff] [blame] | 95 | latex_pkg_list(), |
| 96 | "\\usepackage{graphicx}", |
Hao Zhu | 7039ecf | 2019-01-06 17:51:21 -0500 | [diff] [blame] | 97 | "\\usepackage{xltxtra,xunicode}", |
| 98 | latex_header_includes, |
| 99 | "\\begin{document}", |
| 100 | solve_enc(x), |
| 101 | "\\end{document}" |
| 102 | ) |
| 103 | temp_tex <- paste(temp_tex, collapse = "\n") |
Hao Zhu | 7f8b684 | 2018-10-23 17:41:13 -0400 | [diff] [blame] | 104 | |
Hao Zhu | d851693 | 2019-01-06 18:56:47 -0500 | [diff] [blame] | 105 | temp_tex_file <- paste0(tools::file_path_sans_ext(file), ".tex") |
Hao Zhu | 7039ecf | 2019-01-06 17:51:21 -0500 | [diff] [blame] | 106 | writeLines(temp_tex, temp_tex_file, useBytes = T) |
Hao Zhu | f1873a4 | 2019-01-07 15:57:01 -0500 | [diff] [blame] | 107 | temp_tex_file <- normalizePath(temp_tex_file) |
| 108 | file_no_ext <- tools::file_path_sans_ext(temp_tex_file) |
| 109 | |
| 110 | owd <- setwd(dirname(temp_tex_file)) |
| 111 | |
Hao Zhu | 8902df8 | 2019-01-07 16:27:32 -0500 | [diff] [blame] | 112 | system(paste0("xelatex -interaction=batchmode ", temp_tex_file)) |
Hao Zhu | 7039ecf | 2019-01-06 17:51:21 -0500 | [diff] [blame] | 113 | if (!keep_tex) { |
Hao Zhu | f1873a4 | 2019-01-07 15:57:01 -0500 | [diff] [blame] | 114 | temp_file_delete <- paste0(file_no_ext, c(".tex", ".aux", ".log")) |
Hao Zhu | 7039ecf | 2019-01-06 17:51:21 -0500 | [diff] [blame] | 115 | unlink(temp_file_delete) |
| 116 | } |
| 117 | |
Hao Zhu | f1873a4 | 2019-01-07 15:57:01 -0500 | [diff] [blame] | 118 | table_img_info <- NULL |
Hao Zhu | d851693 | 2019-01-06 18:56:47 -0500 | [diff] [blame] | 119 | if (tools::file_ext(file) != "pdf") { |
| 120 | table_img_pdf <- try( |
Hao Zhu | f1873a4 | 2019-01-07 15:57:01 -0500 | [diff] [blame] | 121 | magick::image_read(paste0(file_no_ext, ".pdf"), |
Hao Zhu | d851693 | 2019-01-06 18:56:47 -0500 | [diff] [blame] | 122 | density = 300), silent = T) |
| 123 | if (class(table_img_pdf) == "try-error") { |
| 124 | stop("We hit an error when trying to use magick to read the generated ", |
Hao Zhu | 53e5961 | 2019-01-15 12:16:06 -0600 | [diff] [blame] | 125 | "PDF file. You may check your magick installation and try to ", |
| 126 | "use magick::image_read to read the PDF file manually. It's also ", |
| 127 | "possible that you didn't have ghostscript installed.") |
Hao Zhu | d851693 | 2019-01-06 18:56:47 -0500 | [diff] [blame] | 128 | } |
Hao Zhu | f1873a4 | 2019-01-07 15:57:01 -0500 | [diff] [blame] | 129 | unlink(paste0(file_no_ext, ".pdf")) |
Hao Zhu | d851693 | 2019-01-06 18:56:47 -0500 | [diff] [blame] | 130 | table_img <- magick::image_convert(table_img_pdf, |
| 131 | tools::file_ext(file)) |
Hao Zhu | f1873a4 | 2019-01-07 15:57:01 -0500 | [diff] [blame] | 132 | table_img_info <- magick::image_info(table_img) |
| 133 | magick::image_write(table_img, |
| 134 | paste0(file_no_ext, ".", tools::file_ext(file))) |
Hao Zhu | 7039ecf | 2019-01-06 17:51:21 -0500 | [diff] [blame] | 135 | } |
Hao Zhu | 7039ecf | 2019-01-06 17:51:21 -0500 | [diff] [blame] | 136 | |
Hao Zhu | f1873a4 | 2019-01-07 15:57:01 -0500 | [diff] [blame] | 137 | setwd(owd) |
| 138 | |
| 139 | out <- paste0(file_no_ext, ".", tools::file_ext(file)) |
| 140 | attr(out, "info") <- table_img_info |
| 141 | return(invisible(out)) |
Hao Zhu | 7f8b684 | 2018-10-23 17:41:13 -0400 | [diff] [blame] | 142 | } |