blob: b81d102a92da2140f498c4aa44595594ce20b10b [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#'
Jiaxiang Lifcb8d452019-04-05 12:05:10 +080024#' @examples
25#' \dontrun{
26#' library(kableExtra)
27#'
28#' kable(mtcars[1:5, ], "html") %>%
29#' kable_styling("striped") %>%
30#' row_spec(1, color = "red") %>%
31#' save_kable("inst/test.pdf")
32#'
33#' kable(mtcars, "latex", booktabs = T) %>%
34#' kable_styling(latex_options = c("striped", "scale_down")) %>%
35#' row_spec(1, color = "red") %>%
36#' as_image()
37#' }
Hao Zhu2a64dc62017-08-28 10:57:57 -040038#' @export
39kable_as_image <- function(kable_input, filename = NULL,
40 file_format = "png",
41 latex_header_includes = NULL,
Hao Zhu4840bc92017-09-15 15:55:05 -040042 keep_pdf = FALSE,
Salzer27eb90b2018-02-11 16:14:04 -050043 density = 300,
44 keep_tex = FALSE) {
Hao Zhuf1873a42019-01-07 15:57:01 -050045 message('kable_as_image is deprecated. Please use save_kable or as_image ',
46 'instead.')
Hao Zhu4840bc92017-09-15 15:55:05 -040047 if (!requireNamespace("magick", quietly = TRUE)) {
48 stop('kable_as_image requires the magick package, which is not available ',
49 'on all platforms. Please get it installed ',
50 'via install.packages("magick"). If you are running on Windows, you ',
51 'also need to install Ghostscript. Please download it here:',
52 'https://ghostscript.com/')
Hao Zhu2a64dc62017-08-28 10:57:57 -040053 } else {
Hao Zhu4840bc92017-09-15 15:55:05 -040054 temp_tex <- c(
55 "\\documentclass[border=1mm, preview]{standalone}",
56 "\\usepackage[active,tightpage]{preview}",
57 "\\usepackage{varwidth}",
58 "\\usepackage{amssymb, amsmath}",
59 "\\usepackage{ifxetex,ifluatex}",
60 "\\usepackage{fixltx2e}",
61 "\\usepackage{polyglossia}",
62 "\\setmainlanguage{$mainlang$}",
63 latex_pkg_list(),
64 "\\usepackage{graphicx}",
65 "\\usepackage{mathspec}",
66 "\\usepackage{xltxtra,xunicode}",
67 latex_header_includes,
68 "\\begin{document}",
Hao Zhu3fc0e882018-04-03 16:06:41 -040069 solve_enc(kable_input),
Hao Zhu4840bc92017-09-15 15:55:05 -040070 "\\end{document}"
71 )
72 temp_tex <- paste(temp_tex, collapse = "\n")
73 temp_file <- paste0("table_", format(Sys.time(), "%Y-%m-%d_%H%M%S"))
antaldaniel2ea01fb2018-05-30 21:20:50 +020074 writeLines(temp_tex, paste0(temp_file, ".tex"), useBytes = T)
Hao Zhu4840bc92017-09-15 15:55:05 -040075 system(paste0("xelatex -interaction=batchmode ", temp_file, ".tex"))
76 temp_file_delete <- paste0(temp_file, c(".tex", ".aux", ".log"))
Salzer27eb90b2018-02-11 16:14:04 -050077 if(!keep_tex) {
78 unlink(temp_file_delete)
79 }
Hao Zhu2a64dc62017-08-28 10:57:57 -040080
Hao Zhu4840bc92017-09-15 15:55:05 -040081 table_img_pdf <- try(magick::image_read(paste0(temp_file, ".pdf"),
82 density = density),
83 silent = T)
84 if (class(table_img_pdf) == "try-error") {
85 stop("Ghostscript is required to read PDF on windows. ",
86 "Please download it here: https://ghostscript.com/")
87 }
88 if (!keep_pdf) {
89 unlink(paste0(temp_file, ".pdf"))
90 }
91 table_img <- magick::image_convert(table_img_pdf, file_format)
92 if (!is.null(filename)) {
93 temp_img <- paste0(filename, ".", file_format)
94 } else {
95 temp_img <- tempfile(fileext = paste0(".", file_format))
96 }
97 magick::image_write(table_img, temp_img)
98
99 include_graphics(temp_img)
100 }
Hao Zhu2a64dc62017-08-28 10:57:57 -0400101}