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`. |
Jiaxiang Li | e4b24f2 | 2019-04-05 13:38:11 +0800 | [diff] [blame] | 21 | #' @examples |
| 22 | #' \dontrun{ |
| 23 | #' library(kableExtra) |
Hao Zhu | 73cf373 | 2018-05-11 17:50:05 -0400 | [diff] [blame] | 24 | #' |
Jiaxiang Li | e4b24f2 | 2019-04-05 13:38:11 +0800 | [diff] [blame] | 25 | #' kable(mtcars[1:5, ], "html") %>% |
| 26 | #' kable_styling("striped") %>% |
| 27 | #' row_spec(1, color = "red") %>% |
| 28 | #' save_kable("inst/test.pdf") |
| 29 | #' } |
Hao Zhu | 73cf373 | 2018-05-11 17:50:05 -0400 | [diff] [blame] | 30 | #' @export |
| 31 | save_kable <- function(x, file, |
Hao Zhu | 7039ecf | 2019-01-06 17:51:21 -0500 | [diff] [blame] | 32 | bs_theme = "simplex", self_contained = TRUE, |
| 33 | extra_dependencies = NULL, ..., |
| 34 | latex_header_includes = NULL, keep_tex = FALSE) { |
Hao Zhu | d851693 | 2019-01-06 18:56:47 -0500 | [diff] [blame] | 35 | if (!is.null(attr(x, "format")) && attr(x, "format") == "latex") { |
Hao Zhu | 7039ecf | 2019-01-06 17:51:21 -0500 | [diff] [blame] | 36 | return(save_kable_latex(x, file, latex_header_includes, keep_tex)) |
Hao Zhu | 7f8b684 | 2018-10-23 17:41:13 -0400 | [diff] [blame] | 37 | } |
Hao Zhu | 7039ecf | 2019-01-06 17:51:21 -0500 | [diff] [blame] | 38 | return(save_kable_html(x, file, bs_theme, self_contained, |
| 39 | extra_dependencies, ...)) |
Hao Zhu | 7f8b684 | 2018-10-23 17:41:13 -0400 | [diff] [blame] | 40 | } |
| 41 | |
Hao Zhu | 7039ecf | 2019-01-06 17:51:21 -0500 | [diff] [blame] | 42 | save_kable_html <- function(x, file, bs_theme, self_contained, |
| 43 | extra_dependencies, ...) { |
| 44 | dependencies <- list( |
Hao Zhu | 73cf373 | 2018-05-11 17:50:05 -0400 | [diff] [blame] | 45 | rmarkdown::html_dependency_jquery(), |
| 46 | rmarkdown::html_dependency_bootstrap(theme = bs_theme), |
| 47 | html_dependency_kePrint() |
| 48 | ) |
Hao Zhu | 7039ecf | 2019-01-06 17:51:21 -0500 | [diff] [blame] | 49 | if (!is.null(extra_dependencies)) { |
| 50 | dependencies <- append(dependencies, extra_dependencies) |
| 51 | } |
| 52 | |
Hao Zhu | 46a42a1 | 2019-01-22 00:11:11 -0500 | [diff] [blame] | 53 | html_header <- htmltools::tags$head(dependencies) |
Hao Zhu | 73cf373 | 2018-05-11 17:50:05 -0400 | [diff] [blame] | 54 | html_table <- htmltools::HTML(as.character(x)) |
| 55 | html_result <- htmltools::tagList(html_header, html_table) |
Hao Zhu | 7f8b684 | 2018-10-23 17:41:13 -0400 | [diff] [blame] | 56 | |
ghaarsma | 8e786ad | 2019-02-16 21:28:01 -0600 | [diff] [blame] | 57 | |
| 58 | # Check if we are generating an image and use webshot to do that |
Hao Zhu | 7f8b684 | 2018-10-23 17:41:13 -0400 | [diff] [blame] | 59 | if (tools::file_ext(file) %in% c("png", "jpg", "jpeg", "pdf")) { |
ghaarsma | 8e786ad | 2019-02-16 21:28:01 -0600 | [diff] [blame] | 60 | file_temp_html <- tempfile(pattern = tools::file_path_sans_ext(file), tmpdir = '.', fileext = ".html") |
| 61 | |
| 62 | file.create(file_temp_html) |
| 63 | file_temp_html <- normalizePath(file_temp_html) |
| 64 | file.create(file) |
| 65 | file <- normalizePath(file) |
| 66 | |
| 67 | # Generate a random temp lib directory. The sub is to remove any back or forward slash at the beginning of the temp_dir |
| 68 | temp_dir <- sub(pattern = '^[\\\\/]{1,2}', replacement = '', tempfile(pattern = 'lib', tmpdir = '' , fileext = '')) |
| 69 | htmltools::save_html(html_result, file = file_temp_html, libdir = temp_dir) |
| 70 | |
| 71 | result <- webshot::webshot(file_temp_html, file, ...) |
| 72 | if (is.null(result)) { |
| 73 | # A webshot could not be created. Delete newly created files and issue msg |
| 74 | file.remove(file) |
| 75 | file.remove(file_temp_html) |
| 76 | message('save_kable could not create image with webshot package. Please check for any webshot messages') |
Hao Zhu | f1873a4 | 2019-01-07 15:57:01 -0500 | [diff] [blame] | 77 | } else { |
ghaarsma | 8e786ad | 2019-02-16 21:28:01 -0600 | [diff] [blame] | 78 | if (tools::file_ext(file) == "pdf") { |
| 79 | message("Note that HTML color may not be displayed on PDF properly.") |
| 80 | } |
| 81 | # Remove temp html file and temp lib directory |
| 82 | file.remove(file_temp_html) |
| 83 | unlink(file.path(dirname(file_temp_html), temp_dir), recursive = TRUE) |
| 84 | |
| 85 | if (requireNamespace("magick", quietly = TRUE)) { |
| 86 | img_rework <- magick::image_read(file) |
| 87 | img_rework <- magick::image_trim(img_rework) |
| 88 | img_info <- magick::image_info(img_rework) |
| 89 | magick::image_write(img_rework, file) |
| 90 | attr(file, "info") <- img_info |
| 91 | } else { |
| 92 | message("save_kable will have the best result with magick installed. ") |
| 93 | } |
Hao Zhu | f1873a4 | 2019-01-07 15:57:01 -0500 | [diff] [blame] | 94 | } |
ghaarsma | 8e786ad | 2019-02-16 21:28:01 -0600 | [diff] [blame] | 95 | |
Hao Zhu | 7f8b684 | 2018-10-23 17:41:13 -0400 | [diff] [blame] | 96 | } else { |
Hao Zhu | f1873a4 | 2019-01-07 15:57:01 -0500 | [diff] [blame] | 97 | file.create(file) |
| 98 | file <- normalizePath(file) |
ghaarsma | 8e786ad | 2019-02-16 21:28:01 -0600 | [diff] [blame] | 99 | |
Hao Zhu | 7f8b684 | 2018-10-23 17:41:13 -0400 | [diff] [blame] | 100 | if (self_contained) { |
ghaarsma | 8e786ad | 2019-02-16 21:28:01 -0600 | [diff] [blame] | 101 | # Generate a random temp lib directory. The sub is to remove any back or forward slash at the beginning of the temp_dir |
| 102 | temp_dir <- sub(pattern = '^[\\\\/]{1,2}', replacement = '', tempfile(pattern = 'lib', tmpdir = '' , fileext = '')) |
| 103 | htmltools::save_html(html_result, file = file, libdir = temp_dir) |
| 104 | #remove_html_doc(file) |
Hao Zhu | 7f8b684 | 2018-10-23 17:41:13 -0400 | [diff] [blame] | 105 | rmarkdown::pandoc_self_contained_html(file, file) |
ghaarsma | 8e786ad | 2019-02-16 21:28:01 -0600 | [diff] [blame] | 106 | unlink(file.path(dirname(file), temp_dir), recursive = TRUE) |
| 107 | } else { |
| 108 | # Simply use the htmltools::save_html to write out the files. Dependencies go to the standard lib folder |
| 109 | htmltools::save_html(html_result, file = file) |
Hao Zhu | 7f8b684 | 2018-10-23 17:41:13 -0400 | [diff] [blame] | 110 | } |
Hao Zhu | 73cf373 | 2018-05-11 17:50:05 -0400 | [diff] [blame] | 111 | } |
Hao Zhu | d851693 | 2019-01-06 18:56:47 -0500 | [diff] [blame] | 112 | |
Hao Zhu | f1873a4 | 2019-01-07 15:57:01 -0500 | [diff] [blame] | 113 | return(invisible(file)) |
Hao Zhu | 73cf373 | 2018-05-11 17:50:05 -0400 | [diff] [blame] | 114 | } |
Hao Zhu | 7f8b684 | 2018-10-23 17:41:13 -0400 | [diff] [blame] | 115 | |
Hao Zhu | 46a42a1 | 2019-01-22 00:11:11 -0500 | [diff] [blame] | 116 | remove_html_doc <- function(x){ |
| 117 | out <- paste(readLines(x)[-1], collapse = "\n") |
| 118 | writeLines(out, x) |
| 119 | } |
| 120 | |
Hao Zhu | 7039ecf | 2019-01-06 17:51:21 -0500 | [diff] [blame] | 121 | save_kable_latex <- function(x, file, latex_header_includes, keep_tex) { |
| 122 | temp_tex <- c( |
| 123 | "\\documentclass[border=1mm, preview]{standalone}", |
| 124 | "\\usepackage[active,tightpage]{preview}", |
| 125 | "\\usepackage{varwidth}", |
| 126 | "\\usepackage{amssymb, amsmath}", |
| 127 | "\\usepackage{ifxetex,ifluatex}", |
| 128 | "\\usepackage{fixltx2e}", |
| 129 | "\\usepackage{polyglossia}", |
Hao Zhu | 7039ecf | 2019-01-06 17:51:21 -0500 | [diff] [blame] | 130 | latex_pkg_list(), |
| 131 | "\\usepackage{graphicx}", |
Hao Zhu | 7039ecf | 2019-01-06 17:51:21 -0500 | [diff] [blame] | 132 | "\\usepackage{xltxtra,xunicode}", |
| 133 | latex_header_includes, |
| 134 | "\\begin{document}", |
| 135 | solve_enc(x), |
| 136 | "\\end{document}" |
| 137 | ) |
| 138 | temp_tex <- paste(temp_tex, collapse = "\n") |
Hao Zhu | 7f8b684 | 2018-10-23 17:41:13 -0400 | [diff] [blame] | 139 | |
Hao Zhu | d851693 | 2019-01-06 18:56:47 -0500 | [diff] [blame] | 140 | temp_tex_file <- paste0(tools::file_path_sans_ext(file), ".tex") |
Hao Zhu | 7039ecf | 2019-01-06 17:51:21 -0500 | [diff] [blame] | 141 | writeLines(temp_tex, temp_tex_file, useBytes = T) |
Hao Zhu | f1873a4 | 2019-01-07 15:57:01 -0500 | [diff] [blame] | 142 | temp_tex_file <- normalizePath(temp_tex_file) |
| 143 | file_no_ext <- tools::file_path_sans_ext(temp_tex_file) |
| 144 | |
| 145 | owd <- setwd(dirname(temp_tex_file)) |
| 146 | |
AC Craft | 7cea533 | 2020-04-19 16:01:07 -0400 | [diff] [blame] | 147 | system(paste0('xelatex -interaction=batchmode "', temp_tex_file,'"')) |
Hao Zhu | 7039ecf | 2019-01-06 17:51:21 -0500 | [diff] [blame] | 148 | if (!keep_tex) { |
Hao Zhu | f1873a4 | 2019-01-07 15:57:01 -0500 | [diff] [blame] | 149 | temp_file_delete <- paste0(file_no_ext, c(".tex", ".aux", ".log")) |
Hao Zhu | 7039ecf | 2019-01-06 17:51:21 -0500 | [diff] [blame] | 150 | unlink(temp_file_delete) |
| 151 | } |
| 152 | |
Hao Zhu | f1873a4 | 2019-01-07 15:57:01 -0500 | [diff] [blame] | 153 | table_img_info <- NULL |
Hao Zhu | d851693 | 2019-01-06 18:56:47 -0500 | [diff] [blame] | 154 | if (tools::file_ext(file) != "pdf") { |
| 155 | table_img_pdf <- try( |
Hao Zhu | f1873a4 | 2019-01-07 15:57:01 -0500 | [diff] [blame] | 156 | magick::image_read(paste0(file_no_ext, ".pdf"), |
Hao Zhu | d851693 | 2019-01-06 18:56:47 -0500 | [diff] [blame] | 157 | density = 300), silent = T) |
| 158 | if (class(table_img_pdf) == "try-error") { |
| 159 | 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] | 160 | "PDF file. You may check your magick installation and try to ", |
| 161 | "use magick::image_read to read the PDF file manually. It's also ", |
| 162 | "possible that you didn't have ghostscript installed.") |
Hao Zhu | d851693 | 2019-01-06 18:56:47 -0500 | [diff] [blame] | 163 | } |
Hao Zhu | f1873a4 | 2019-01-07 15:57:01 -0500 | [diff] [blame] | 164 | unlink(paste0(file_no_ext, ".pdf")) |
Hao Zhu | d851693 | 2019-01-06 18:56:47 -0500 | [diff] [blame] | 165 | table_img <- magick::image_convert(table_img_pdf, |
| 166 | tools::file_ext(file)) |
Hao Zhu | f1873a4 | 2019-01-07 15:57:01 -0500 | [diff] [blame] | 167 | table_img_info <- magick::image_info(table_img) |
| 168 | magick::image_write(table_img, |
| 169 | paste0(file_no_ext, ".", tools::file_ext(file))) |
Hao Zhu | 7039ecf | 2019-01-06 17:51:21 -0500 | [diff] [blame] | 170 | } |
Hao Zhu | 7039ecf | 2019-01-06 17:51:21 -0500 | [diff] [blame] | 171 | |
Hao Zhu | f1873a4 | 2019-01-07 15:57:01 -0500 | [diff] [blame] | 172 | setwd(owd) |
| 173 | |
| 174 | out <- paste0(file_no_ext, ".", tools::file_ext(file)) |
| 175 | attr(out, "info") <- table_img_info |
| 176 | return(invisible(out)) |
Hao Zhu | 7f8b684 | 2018-10-23 17:41:13 -0400 | [diff] [blame] | 177 | } |