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