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 | 23ddaf3 | 2019-01-08 13:42:26 -0500 | [diff] [blame] | 24 | # Turn your tables into images for cross-formating support |
| 25 | `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`. |
| 26 | |
Hao Zhu | 8902df8 | 2019-01-07 16:27:32 -0500 | [diff] [blame] | 27 | ```{r} |
| 28 | library(kableExtra) |
| 29 | |
| 30 | kable(mtcars, "latex", booktabs = T) %>% |
| 31 | kable_styling(latex_options = c("striped", "scale_down")) %>% |
| 32 | row_spec(1, color = "red") %>% |
| 33 | as_image() |
| 34 | ``` |
| 35 | |
Hao Zhu | 7c39cba | 2019-01-08 15:29:33 -0500 | [diff] [blame^] | 36 | 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] | 37 | |
| 38 | ```{r} |
| 39 | kable(mtcars, "latex", booktabs = T) %>% |
| 40 | kable_styling(latex_options = c("striped", "scale_down")) %>% |
| 41 | row_spec(1, color = "red") %>% |
| 42 | as_image(width = 8) |
| 43 | ``` |
| 44 | |
Hao Zhu | 7c39cba | 2019-01-08 15:29:33 -0500 | [diff] [blame^] | 45 | `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 | 8902df8 | 2019-01-07 16:27:32 -0500 | [diff] [blame] | 46 | ```{r} |
| 47 | kable(mtcars, "html") %>% |
| 48 | kable_styling("striped") %>% |
| 49 | row_spec(1, color = "red") %>% |
Hao Zhu | 7c39cba | 2019-01-08 15:29:33 -0500 | [diff] [blame^] | 50 | as_image(width = 4) |
Hao Zhu | 8902df8 | 2019-01-07 16:27:32 -0500 | [diff] [blame] | 51 | ``` |
| 52 | |
Hao Zhu | 7c39cba | 2019-01-08 15:29:33 -0500 | [diff] [blame^] | 53 | One problem with this method, especially if you are using it in `Word`, is that to the end, the product you made is a picture. Please let your readers know that they cannot select/edit the tables. :P |