Hao Zhu | 8902df8 | 2019-01-07 16:27:32 -0500 | [diff] [blame] | 1 | --- |
| 2 | title: "Save Tables and Use them as Images" |
| 3 | author: "Hao" |
| 4 | date: "1/7/2019" |
| 5 | output: html_document |
| 6 | --- |
| 7 | |
| 8 | Before `kableExtra` 1.0, we have a function called `kable_as_image`, which can transform a LaTeX tables to an image and include it in an rmarkdown document. Starting from `kableExtra` 1.0, we will split this function into two pieces: `save_kable` and `as_image`. `save_kable` will save any LaTeX and **HTML** tables to any format (`html`, `png`, `jpg`, `pdf` etc.). The output format will be defined by the file extension of the output file name. Here are a few examples. |
| 9 | |
| 10 | ```{r, eval=FALSE} |
| 11 | # not evaluating |
| 12 | library(kableExtra) |
| 13 | |
| 14 | kable(mtcars[1:5, ], "html") %>% |
| 15 | kable_styling("striped") %>% |
| 16 | row_spec(1, color = "red") %>% |
| 17 | save_kable("inst/test.pdf") |
| 18 | |
| 19 | kable(mtcars, "latex") %>% |
| 20 | kable_styling(latex_options = "striped") %>% |
| 21 | save_kable("inst/test.png") |
| 22 | ``` |
| 23 | |
Hao Zhu | 53e5961 | 2019-01-15 12:16:06 -0600 | [diff] [blame] | 24 | # Feature Dependencies |
| 25 | To get it work, you need to make sure you have the following things installed. |
| 26 | ```{r, eval = F} |
| 27 | install.packages("magick") |
| 28 | install.packages("webshot") |
| 29 | webshot::install_phantomjs() |
| 30 | ``` |
| 31 | |
| 32 | In the end, please make sure you can use `magick::read_image()` function properly for a PDF. On some system (not only windows), it's possible that GhostScript was not setup properly so even if you have `magick` installed, `magick::read_image()` still cannot read PDF properly. That said, although `save_kable` and `as_image` provide really attractive feature, they may not work on every computer. |
| 33 | |
Hao Zhu | 23ddaf3 | 2019-01-08 13:42:26 -0500 | [diff] [blame] | 34 | # Turn your tables into images for cross-formating support |
| 35 | `kableExtra` only supports `HTML` and `LaTeX`. One way to get it work with `Word` is to render the table `as_image`. This example below shows you how to render a LaTeX table in HTML document. Same rules applies when you use them in `rmarkdown::word_document`. |
| 36 | |
Hao Zhu | 8902df8 | 2019-01-07 16:27:32 -0500 | [diff] [blame] | 37 | ```{r} |
| 38 | library(kableExtra) |
| 39 | |
| 40 | kable(mtcars, "latex", booktabs = T) %>% |
| 41 | kable_styling(latex_options = c("striped", "scale_down")) %>% |
| 42 | row_spec(1, color = "red") %>% |
| 43 | as_image() |
| 44 | ``` |
| 45 | |
Hao Zhu | 7c39cba | 2019-01-08 15:29:33 -0500 | [diff] [blame] | 46 | You can also specify either `width` or `height` of the image. The image will be scaled properly. The units are in inches. |
Hao Zhu | 8902df8 | 2019-01-07 16:27:32 -0500 | [diff] [blame] | 47 | |
| 48 | ```{r} |
| 49 | kable(mtcars, "latex", booktabs = T) %>% |
| 50 | kable_styling(latex_options = c("striped", "scale_down")) %>% |
| 51 | row_spec(1, color = "red") %>% |
| 52 | as_image(width = 8) |
| 53 | ``` |
| 54 | |
Hao Zhu | 7c39cba | 2019-01-08 15:29:33 -0500 | [diff] [blame] | 55 | `as_image` also works for HTML tables. It means that you can also put in a bootstrap flavored table (image) in Word or PDF. |
Hao Zhu | 53e5961 | 2019-01-15 12:16:06 -0600 | [diff] [blame] | 56 | |
Hao Zhu | 8902df8 | 2019-01-07 16:27:32 -0500 | [diff] [blame] | 57 | ```{r} |
| 58 | kable(mtcars, "html") %>% |
| 59 | kable_styling("striped") %>% |
| 60 | row_spec(1, color = "red") %>% |
Hao Zhu | 7c39cba | 2019-01-08 15:29:33 -0500 | [diff] [blame] | 61 | as_image(width = 4) |
Hao Zhu | 8902df8 | 2019-01-07 16:27:32 -0500 | [diff] [blame] | 62 | ``` |
| 63 | |
Hao Zhu | b361fb5 | 2019-01-15 12:24:10 -0600 | [diff] [blame] | 64 | Note that, 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 the `file` option in `as_image` be the path you need, eg. "img/something.png" |
Hao Zhu | 37975a3 | 2019-01-15 14:59:34 -0600 | [diff] [blame] | 65 | |