Hao Zhu | 2a64dc6 | 2017-08-28 10:57:57 -0400 | [diff] [blame] | 1 | #' Convert a LaTeX table to an image and place it in a rmarkdown document |
| 2 | #' |
| 3 | #' @description This is a LaTeX-only function. This function will render the |
| 4 | #' raw LaTeX code (could be codes generated by other table packages like |
| 5 | #' `xtable`) to generate a table, convert it to an image and put it back to a |
| 6 | #' rmarkdown environment. It is a "better than nothing" solution to print high |
| 7 | #' quality tables in rmarkdown Word document. By using this, you need to take |
Hao Zhu | 245931c | 2017-09-01 22:43:56 -0400 | [diff] [blame] | 8 | #' the responsibility of explaining to your collaborators why they can't make |
Hao Zhu | a9c43dc | 2017-09-04 23:00:39 -0400 | [diff] [blame] | 9 | #' edits to the tables in Word. |
Hao Zhu | 2a64dc6 | 2017-08-28 10:57:57 -0400 | [diff] [blame] | 10 | #' |
| 11 | #' Also, if a filename is provided, user has the option to "save" the table to |
| 12 | #' an image file like `ggplot2::ggsave()`. |
| 13 | #' |
| 14 | #' The idea of this function was coming from [this StackOverflow question](https://stackoverflow.com/questions/44711313/save-rmarkdowns-report-tables-and-figures-to-file). |
| 15 | #' The approach was learned and adopted from the [texpreview](https://github.com/metrumresearchgroup/texPreview) |
| 16 | #' package, which allows you to preview the results of TeX code in the Viewer panel. |
| 17 | #' |
| 18 | #' @param kable_input Raw LaTeX code to generate a table. It doesn't have to |
| 19 | #' came from `kable` or `kableExtra`. |
| 20 | #' @param filename Character String. If specified, the image will be saved under |
| 21 | #' the specified (path &) name. You don't need to put file format like ".png" |
| 22 | #' here. |
| 23 | #' @param file_format Character String to specify image format, such as `png`, |
| 24 | #' `jpeg`, `gif`, `tiff`, etc. Default is `png`. |
| 25 | #' @param latex_header_includes A character vector of extra LaTeX header stuff. |
| 26 | #' Each element is a row. You can have things like |
| 27 | #' `c("\\usepackage{threeparttable}", "\\usepackage{icons}")` |
| 28 | #' @param keep_pdf A T/F option to control if the mid-way standalone pdf should |
| 29 | #' be kept. Default is `FALSE`. |
| 30 | #' |
Hao Zhu | 2a64dc6 | 2017-08-28 10:57:57 -0400 | [diff] [blame] | 31 | #' @export |
| 32 | kable_as_image <- function(kable_input, filename = NULL, |
| 33 | file_format = "png", |
| 34 | latex_header_includes = NULL, |
| 35 | keep_pdf = FALSE) { |
| 36 | temp_tex <- c( |
| 37 | "\\documentclass[border=1mm, preview]{standalone}", |
| 38 | "\\usepackage[active,tightpage]{preview}", |
| 39 | "\\usepackage{varwidth}", |
Hao Zhu | a9c43dc | 2017-09-04 23:00:39 -0400 | [diff] [blame] | 40 | "\\usepackage{amssymb, amsmath}", |
Hao Zhu | 2a64dc6 | 2017-08-28 10:57:57 -0400 | [diff] [blame] | 41 | "\\usepackage{ifxetex,ifluatex}", |
| 42 | "\\usepackage{fixltx2e}", |
| 43 | "\\usepackage{polyglossia}", |
| 44 | "\\setmainlanguage{$mainlang$}", |
| 45 | "\\usepackage{booktabs}", |
| 46 | "\\usepackage{longtable}", |
| 47 | "\\usepackage{array}", |
| 48 | "\\usepackage{multirow}", |
| 49 | "\\usepackage[table]{xcolor}", |
| 50 | "\\usepackage{wrapfig}", |
| 51 | "\\usepackage{colortbl}", |
| 52 | "\\usepackage{graphicx}", |
| 53 | "\\usepackage{mathspec}", |
| 54 | "\\usepackage{xltxtra,xunicode}", |
| 55 | latex_header_includes, |
| 56 | "\\begin{document}", |
| 57 | enc2utf8(as.character(kable_input)), |
| 58 | "\\end{document}" |
| 59 | ) |
| 60 | temp_tex <- paste(temp_tex, collapse = "\n") |
Hao Zhu | cc39792 | 2017-09-14 11:49:21 -0400 | [diff] [blame^] | 61 | temp_file <- paste0("table_", format(Sys.time(), "%Y-%m-%d_%H%M%S")) |
Hao Zhu | 2a64dc6 | 2017-08-28 10:57:57 -0400 | [diff] [blame] | 62 | write_file(temp_tex, paste0(temp_file, ".tex")) |
| 63 | system(paste0("xelatex -interaction=batchmode ", temp_file, ".tex")) |
| 64 | temp_file_delete <- paste0(temp_file, c(".tex", ".aux", ".log")) |
| 65 | unlink(temp_file_delete) |
| 66 | |
| 67 | table_img_pdf <- image_read(paste0(temp_file, ".pdf"), density = 300) |
| 68 | if (!keep_pdf) { |
| 69 | unlink(paste0(temp_file, ".pdf")) |
| 70 | } |
| 71 | table_img <- image_convert(table_img_pdf, file_format) |
| 72 | if (!is.null(filename)) { |
| 73 | temp_img <- paste0(filename, ".", file_format) |
| 74 | } else { |
| 75 | temp_img <- tempfile(fileext = paste0(".", file_format)) |
| 76 | } |
| 77 | image_write(table_img, temp_img) |
| 78 | |
| 79 | include_graphics(temp_img) |
| 80 | } |