| #' Render the table as an format-independent image and use it in rmarkdown |
| #' |
| #' @description This function generates a temporary png file using `save_kable` |
| #' and then try to put it in an rmarkdown document using |
| #' `knitr::include_graphics`. |
| #' |
| #' @param x kable input. Either HTML or LaTeX |
| #' @param width Image width in inches. (1 inch = 2.54 cm) |
| #' @param height Image height in inches. (1 inch = 2.54 cm) |
| #' @param file By default, as_image saves to an temp file, which works for |
| #' normal rmarkdown. However if you are using things like xaringan, which can't |
| #' be a standalone html, you can specify this file be the path you need, eg. |
| #' "img/something.png" |
| #' |
| #' @param ... Additional arguments passed to save_kable. |
| #' |
| #' |
| #' @export |
| as_image <- function(x, width = NULL, height = NULL, file = NULL, ...) { |
| if (is.null(width) + is.null(height) == 0) { |
| message("Both width and height were defined. Use width only by default. ") |
| height <- NULL |
| } |
| |
| if (is.null(file)) { |
| temp_png <- tempfile(fileext = ".png") |
| } else { |
| temp_png <- file |
| } |
| |
| |
| temp_img <- save_kable(x = x, file = temp_png, ...) |
| |
| img_dpi <- 300 |
| |
| if (is.null(width) + is.null(height) <= 1 & is.null(attr(temp_img, "info"))) { |
| warning("You need to install magick in order to use width/height in ", |
| "as_image. ") |
| } else { |
| if (!is.null(width)) { |
| img_dpi <- attr(temp_img, "info")$width / width |
| } |
| if (!is.null(height)) { |
| img_dpi <- attr(temp_img, "info")$height / height |
| } |
| } |
| |
| include_graphics(temp_png, dpi = img_dpi) |
| } |
| |
| |