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 |
| 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 | b0fd4ef | 2017-09-04 20:19:00 -0400 | [diff] [blame] | 31 | #' @examples kable_as_image(knitr::kable(mtcars, "latex", booktabs = TRUE), "mtcars") |
| 32 | #' |
Hao Zhu | 2a64dc6 | 2017-08-28 10:57:57 -0400 | [diff] [blame] | 33 | #' @export |
| 34 | kable_as_image <- function(kable_input, filename = NULL, |
| 35 | file_format = "png", |
| 36 | latex_header_includes = NULL, |
| 37 | keep_pdf = FALSE) { |
| 38 | temp_tex <- c( |
| 39 | "\\documentclass[border=1mm, preview]{standalone}", |
| 40 | "\\usepackage[active,tightpage]{preview}", |
| 41 | "\\usepackage{varwidth}", |
| 42 | "\\usepackage{amssymb,amsmath}", |
| 43 | "\\usepackage{ifxetex,ifluatex}", |
| 44 | "\\usepackage{fixltx2e}", |
| 45 | "\\usepackage{polyglossia}", |
| 46 | "\\setmainlanguage{$mainlang$}", |
| 47 | "\\usepackage{booktabs}", |
| 48 | "\\usepackage{longtable}", |
| 49 | "\\usepackage{array}", |
| 50 | "\\usepackage{multirow}", |
| 51 | "\\usepackage[table]{xcolor}", |
| 52 | "\\usepackage{wrapfig}", |
| 53 | "\\usepackage{colortbl}", |
| 54 | "\\usepackage{graphicx}", |
| 55 | "\\usepackage{mathspec}", |
| 56 | "\\usepackage{xltxtra,xunicode}", |
| 57 | latex_header_includes, |
| 58 | "\\begin{document}", |
| 59 | enc2utf8(as.character(kable_input)), |
| 60 | "\\end{document}" |
| 61 | ) |
| 62 | temp_tex <- paste(temp_tex, collapse = "\n") |
| 63 | temp_file <- paste0("table_", format(Sys.time(), "%Y-%m-%d_%H:%M:%S")) |
| 64 | write_file(temp_tex, paste0(temp_file, ".tex")) |
| 65 | system(paste0("xelatex -interaction=batchmode ", temp_file, ".tex")) |
| 66 | temp_file_delete <- paste0(temp_file, c(".tex", ".aux", ".log")) |
| 67 | unlink(temp_file_delete) |
| 68 | |
| 69 | table_img_pdf <- image_read(paste0(temp_file, ".pdf"), density = 300) |
| 70 | if (!keep_pdf) { |
| 71 | unlink(paste0(temp_file, ".pdf")) |
| 72 | } |
| 73 | table_img <- image_convert(table_img_pdf, file_format) |
| 74 | if (!is.null(filename)) { |
| 75 | temp_img <- paste0(filename, ".", file_format) |
| 76 | } else { |
| 77 | temp_img <- tempfile(fileext = paste0(".", file_format)) |
| 78 | } |
| 79 | image_write(table_img, temp_img) |
| 80 | |
| 81 | include_graphics(temp_img) |
| 82 | } |