blob: ffe18be22223738c389590c770b9c31e293a5e90 [file] [log] [blame]
Hao Zhu8902df82019-01-07 16:27:32 -05001#' Deprecated
Hao Zhu2a64dc62017-08-28 10:57:57 -04002#'
Hao Zhu8902df82019-01-07 16:27:32 -05003#' @description deprecated
Hao Zhu2a64dc62017-08-28 10:57:57 -04004#'
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
antaldaniel8d1bd5b2018-05-30 21:43:33 +020014#' `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 Zhu2a64dc62017-08-28 10:57:57 -040017#' @param keep_pdf A T/F option to control if the mid-way standalone pdf should
18#' be kept. Default is `FALSE`.
Hao Zhu4840bc92017-09-15 15:55:05 -040019#' @param density Resolution to read the PDF file. Default value is 300, which
20#' should be sufficient in most cases.
Salzer27eb90b2018-02-11 16:14:04 -050021#' @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 Zhu2a64dc62017-08-28 10:57:57 -040023#'
Hao Zhu2a64dc62017-08-28 10:57:57 -040024#' @export
25kable_as_image <- function(kable_input, filename = NULL,
26 file_format = "png",
27 latex_header_includes = NULL,
Hao Zhu4840bc92017-09-15 15:55:05 -040028 keep_pdf = FALSE,
Salzer27eb90b2018-02-11 16:14:04 -050029 density = 300,
30 keep_tex = FALSE) {
Hao Zhuf1873a42019-01-07 15:57:01 -050031 message('kable_as_image is deprecated. Please use save_kable or as_image ',
32 'instead.')
Hao Zhu4840bc92017-09-15 15:55:05 -040033 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 Zhu2a64dc62017-08-28 10:57:57 -040039 } else {
Hao Zhu4840bc92017-09-15 15:55:05 -040040 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 Zhu3fc0e882018-04-03 16:06:41 -040055 solve_enc(kable_input),
Hao Zhu4840bc92017-09-15 15:55:05 -040056 "\\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"))
antaldaniel2ea01fb2018-05-30 21:20:50 +020060 writeLines(temp_tex, paste0(temp_file, ".tex"), useBytes = T)
Hao Zhu4840bc92017-09-15 15:55:05 -040061 system(paste0("xelatex -interaction=batchmode ", temp_file, ".tex"))
62 temp_file_delete <- paste0(temp_file, c(".tex", ".aux", ".log"))
Salzer27eb90b2018-02-11 16:14:04 -050063 if(!keep_tex) {
64 unlink(temp_file_delete)
65 }
Hao Zhu2a64dc62017-08-28 10:57:57 -040066
Hao Zhu4840bc92017-09-15 15:55:05 -040067 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 Zhu2a64dc62017-08-28 10:57:57 -040087}