blob: ff030cccdf390411d15f410874d7b87255095837 [file] [log] [blame]
#' Convert a LaTeX table to an image and place it in a rmarkdown document
#'
#' @description This is a LaTeX-only function. This function will render the
#' raw LaTeX code (could be codes generated by other table packages like
#' `xtable`) to generate a table, convert it to an image and put it back to a
#' rmarkdown environment. It is a "better than nothing" solution to print high
#' quality tables in rmarkdown Word document. By using this, you need to take
#' the responsibility of explaining to your collaborators why they can't make
#' edits to the tables in Word. 😂
#'
#' Also, if a filename is provided, user has the option to "save" the table to
#' an image file like `ggplot2::ggsave()`.
#'
#' The idea of this function was coming from [this StackOverflow question](https://stackoverflow.com/questions/44711313/save-rmarkdowns-report-tables-and-figures-to-file).
#' The approach was learned and adopted from the [texpreview](https://github.com/metrumresearchgroup/texPreview)
#' package, which allows you to preview the results of TeX code in the Viewer panel.
#'
#' @param kable_input Raw LaTeX code to generate a table. It doesn't have to
#' came from `kable` or `kableExtra`.
#' @param filename Character String. If specified, the image will be saved under
#' the specified (path &) name. You don't need to put file format like ".png"
#' here.
#' @param file_format Character String to specify image format, such as `png`,
#' `jpeg`, `gif`, `tiff`, etc. Default is `png`.
#' @param latex_header_includes A character vector of extra LaTeX header stuff.
#' Each element is a row. You can have things like
#' `c("\\usepackage{threeparttable}", "\\usepackage{icons}")`
#' @param keep_pdf A T/F option to control if the mid-way standalone pdf should
#' be kept. Default is `FALSE`.
#'
#' @examples kable_as_image(kanitr::kble(mtcars, "latex"), "mtcars")
#' @export
kable_as_image <- function(kable_input, filename = NULL,
file_format = "png",
latex_header_includes = NULL,
keep_pdf = FALSE) {
temp_tex <- c(
"\\documentclass[border=1mm, preview]{standalone}",
"\\usepackage[active,tightpage]{preview}",
"\\usepackage{varwidth}",
"\\usepackage{amssymb,amsmath}",
"\\usepackage{ifxetex,ifluatex}",
"\\usepackage{fixltx2e}",
"\\usepackage{polyglossia}",
"\\setmainlanguage{$mainlang$}",
"\\usepackage{booktabs}",
"\\usepackage{longtable}",
"\\usepackage{array}",
"\\usepackage{multirow}",
"\\usepackage[table]{xcolor}",
"\\usepackage{wrapfig}",
"\\usepackage{colortbl}",
"\\usepackage{graphicx}",
"\\usepackage{mathspec}",
"\\usepackage{xltxtra,xunicode}",
latex_header_includes,
"\\begin{document}",
enc2utf8(as.character(kable_input)),
"\\end{document}"
)
temp_tex <- paste(temp_tex, collapse = "\n")
temp_file <- paste0("table_", format(Sys.time(), "%Y-%m-%d_%H:%M:%S"))
write_file(temp_tex, paste0(temp_file, ".tex"))
system(paste0("xelatex -interaction=batchmode ", temp_file, ".tex"))
temp_file_delete <- paste0(temp_file, c(".tex", ".aux", ".log"))
unlink(temp_file_delete)
table_img_pdf <- image_read(paste0(temp_file, ".pdf"), density = 300)
if (!keep_pdf) {
unlink(paste0(temp_file, ".pdf"))
}
table_img <- image_convert(table_img_pdf, file_format)
if (!is.null(filename)) {
temp_img <- paste0(filename, ".", file_format)
} else {
temp_img <- tempfile(fileext = paste0(".", file_format))
}
image_write(table_img, temp_img)
include_graphics(temp_img)
}