Hao Zhu | 8902df8 | 2019-01-07 16:27:32 -0500 | [diff] [blame^] | 1 | #' Deprecated |
Hao Zhu | 2a64dc6 | 2017-08-28 10:57:57 -0400 | [diff] [blame] | 2 | #' |
Hao Zhu | 8902df8 | 2019-01-07 16:27:32 -0500 | [diff] [blame^] | 3 | #' @description deprecated |
Hao Zhu | 2a64dc6 | 2017-08-28 10:57:57 -0400 | [diff] [blame] | 4 | #' |
| 5 | #' @param kable_input Raw LaTeX code to generate a table. It doesn't have to |
| 6 | #' came from `kable` or `kableExtra`. |
| 7 | #' @param filename Character String. If specified, the image will be saved under |
| 8 | #' the specified (path &) name. You don't need to put file format like ".png" |
| 9 | #' here. |
| 10 | #' @param file_format Character String to specify image format, such as `png`, |
| 11 | #' `jpeg`, `gif`, `tiff`, etc. Default is `png`. |
| 12 | #' @param latex_header_includes A character vector of extra LaTeX header stuff. |
| 13 | #' Each element is a row. You can have things like |
antaldaniel | 8d1bd5b | 2018-05-30 21:43:33 +0200 | [diff] [blame] | 14 | #' `c("\\\\usepackage{threeparttable}", "\\\\usepackage{icons}")` You could |
| 15 | #' probably add your language package here if you use non-English text in your |
| 16 | #' table, such as `\\\\usepackage[magyar]{babel}`. |
Hao Zhu | 2a64dc6 | 2017-08-28 10:57:57 -0400 | [diff] [blame] | 17 | #' @param keep_pdf A T/F option to control if the mid-way standalone pdf should |
| 18 | #' be kept. Default is `FALSE`. |
Hao Zhu | 4840bc9 | 2017-09-15 15:55:05 -0400 | [diff] [blame] | 19 | #' @param density Resolution to read the PDF file. Default value is 300, which |
| 20 | #' should be sufficient in most cases. |
Salzer | 27eb90b | 2018-02-11 16:14:04 -0500 | [diff] [blame] | 21 | #' @param keep_tex A T/F option to control if the latex file that is initially created |
| 22 | #' should be kept. Default is `FALSE`. |
Hao Zhu | 2a64dc6 | 2017-08-28 10:57:57 -0400 | [diff] [blame] | 23 | #' |
Hao Zhu | 2a64dc6 | 2017-08-28 10:57:57 -0400 | [diff] [blame] | 24 | #' @export |
| 25 | kable_as_image <- function(kable_input, filename = NULL, |
| 26 | file_format = "png", |
| 27 | latex_header_includes = NULL, |
Hao Zhu | 4840bc9 | 2017-09-15 15:55:05 -0400 | [diff] [blame] | 28 | keep_pdf = FALSE, |
Salzer | 27eb90b | 2018-02-11 16:14:04 -0500 | [diff] [blame] | 29 | density = 300, |
| 30 | keep_tex = FALSE) { |
Hao Zhu | f1873a4 | 2019-01-07 15:57:01 -0500 | [diff] [blame] | 31 | message('kable_as_image is deprecated. Please use save_kable or as_image ', |
| 32 | 'instead.') |
Hao Zhu | 4840bc9 | 2017-09-15 15:55:05 -0400 | [diff] [blame] | 33 | if (!requireNamespace("magick", quietly = TRUE)) { |
| 34 | stop('kable_as_image requires the magick package, which is not available ', |
| 35 | 'on all platforms. Please get it installed ', |
| 36 | 'via install.packages("magick"). If you are running on Windows, you ', |
| 37 | 'also need to install Ghostscript. Please download it here:', |
| 38 | 'https://ghostscript.com/') |
Hao Zhu | 2a64dc6 | 2017-08-28 10:57:57 -0400 | [diff] [blame] | 39 | } else { |
Hao Zhu | 4840bc9 | 2017-09-15 15:55:05 -0400 | [diff] [blame] | 40 | temp_tex <- c( |
| 41 | "\\documentclass[border=1mm, preview]{standalone}", |
| 42 | "\\usepackage[active,tightpage]{preview}", |
| 43 | "\\usepackage{varwidth}", |
| 44 | "\\usepackage{amssymb, amsmath}", |
| 45 | "\\usepackage{ifxetex,ifluatex}", |
| 46 | "\\usepackage{fixltx2e}", |
| 47 | "\\usepackage{polyglossia}", |
| 48 | "\\setmainlanguage{$mainlang$}", |
| 49 | latex_pkg_list(), |
| 50 | "\\usepackage{graphicx}", |
| 51 | "\\usepackage{mathspec}", |
| 52 | "\\usepackage{xltxtra,xunicode}", |
| 53 | latex_header_includes, |
| 54 | "\\begin{document}", |
Hao Zhu | 3fc0e88 | 2018-04-03 16:06:41 -0400 | [diff] [blame] | 55 | solve_enc(kable_input), |
Hao Zhu | 4840bc9 | 2017-09-15 15:55:05 -0400 | [diff] [blame] | 56 | "\\end{document}" |
| 57 | ) |
| 58 | temp_tex <- paste(temp_tex, collapse = "\n") |
| 59 | temp_file <- paste0("table_", format(Sys.time(), "%Y-%m-%d_%H%M%S")) |
antaldaniel | 2ea01fb | 2018-05-30 21:20:50 +0200 | [diff] [blame] | 60 | writeLines(temp_tex, paste0(temp_file, ".tex"), useBytes = T) |
Hao Zhu | 4840bc9 | 2017-09-15 15:55:05 -0400 | [diff] [blame] | 61 | system(paste0("xelatex -interaction=batchmode ", temp_file, ".tex")) |
| 62 | temp_file_delete <- paste0(temp_file, c(".tex", ".aux", ".log")) |
Salzer | 27eb90b | 2018-02-11 16:14:04 -0500 | [diff] [blame] | 63 | if(!keep_tex) { |
| 64 | unlink(temp_file_delete) |
| 65 | } |
Hao Zhu | 2a64dc6 | 2017-08-28 10:57:57 -0400 | [diff] [blame] | 66 | |
Hao Zhu | 4840bc9 | 2017-09-15 15:55:05 -0400 | [diff] [blame] | 67 | table_img_pdf <- try(magick::image_read(paste0(temp_file, ".pdf"), |
| 68 | density = density), |
| 69 | silent = T) |
| 70 | if (class(table_img_pdf) == "try-error") { |
| 71 | stop("Ghostscript is required to read PDF on windows. ", |
| 72 | "Please download it here: https://ghostscript.com/") |
| 73 | } |
| 74 | if (!keep_pdf) { |
| 75 | unlink(paste0(temp_file, ".pdf")) |
| 76 | } |
| 77 | table_img <- magick::image_convert(table_img_pdf, file_format) |
| 78 | if (!is.null(filename)) { |
| 79 | temp_img <- paste0(filename, ".", file_format) |
| 80 | } else { |
| 81 | temp_img <- tempfile(fileext = paste0(".", file_format)) |
| 82 | } |
| 83 | magick::image_write(table_img, temp_img) |
| 84 | |
| 85 | include_graphics(temp_img) |
| 86 | } |
Hao Zhu | 2a64dc6 | 2017-08-28 10:57:57 -0400 | [diff] [blame] | 87 | } |