Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 1 | --- |
| 2 | title: "Use kableExtra with formattable" |
| 3 | author: "Hao Zhu" |
| 4 | date: "`r Sys.Date()`" |
| 5 | output: html_document |
Hao Zhu | 52531d8 | 2018-01-14 23:54:55 -0500 | [diff] [blame] | 6 | vignette: > |
| 7 | %\VignetteIndexEntry{Use kableExtra with formattable} |
| 8 | %\VignetteEngine{knitr::rmarkdown} |
| 9 | %\VignetteEncoding{UTF-8} |
Hao Zhu | bacd2f3 | 2017-10-11 14:06:36 -0400 | [diff] [blame] | 10 | --- |
| 11 | |
| 12 | I know you love `formattable`'s `color_tile` and `color_bar`. I do too. In my mind, these two features are among the best visualization methods in a tabular format. You may wonder if it's possible use it together with `kableExtra` so you can have a nested table with awesome presentation at the same time. Here is how. |
| 13 | |
| 14 | ```{r, message=FALSE, warning=FALSE} |
| 15 | library(knitr) |
| 16 | library(kableExtra) |
| 17 | library(formattable) |
| 18 | library(dplyr) |
| 19 | ``` |
| 20 | |
| 21 | ```{r} |
| 22 | mtcars[1:5, 1:4] %>% |
| 23 | mutate( |
| 24 | car = row.names(.), |
| 25 | mpg = color_tile("white", "orange")(mpg), |
| 26 | cyl = cell_spec(cyl, "html", angle = (1:5)*60, |
| 27 | background = "red", color = "white", align = "center"), |
| 28 | disp = ifelse(disp > 200, |
| 29 | cell_spec(disp, "html", color = "red", bold = T), |
| 30 | cell_spec(disp, "html", color = "green", italic = T)), |
| 31 | hp = color_bar("lightgreen")(hp) |
| 32 | ) %>% |
| 33 | select(car, everything()) %>% |
| 34 | kable("html", escape = F) %>% |
| 35 | kable_styling("hover", full_width = F) %>% |
| 36 | column_spec(5, width = "3cm") %>% |
| 37 | add_header_above(c(" ", "Hello" = 2, "World" = 2)) |
| 38 | ``` |
| 39 | |
| 40 | Here is a little bit more explanation. Both `formattable::color_tile` and `formattable::color_bar` returns a function object which can take a numeric vector. That's why you can put things like `(mpg)` after `color_tile("white", "orange")` as you can see in the `mutate` function. This way of using the function may look a little odd but is totally valid (, I think :P). |
| 41 | |
| 42 | Also, if you are using it in this way, make sure you put `escape = F` in `kable`. |
| 43 | |
Hao Zhu | 6290fdd | 2017-10-24 00:10:32 -0400 | [diff] [blame] | 44 | On the other hand, `cell_spec()` is a new function in `kableExtra()` to format cells _**before you pipe the table into `kable`**_. Note that you can either specify format (`html` or `latex`) in function or do that via `options(knitr.table.format)` so you don't need to do it everytime. Please refer to the package vignette for details. |
Hao Zhu | 457acb4 | 2017-10-14 17:37:02 -0400 | [diff] [blame] | 45 | |
Hao Zhu | 6290fdd | 2017-10-24 00:10:32 -0400 | [diff] [blame] | 46 | <script> |
| 47 | $(document).ready(function(){ |
| 48 | $('[data-toggle="tooltip"]').tooltip(); |
| 49 | }); |
| 50 | </script> |
Hao Zhu | 457acb4 | 2017-10-14 17:37:02 -0400 | [diff] [blame] | 51 | |
| 52 | ```{r} |
| 53 | iris[1:10, ] %>% |
Hao Zhu | 6290fdd | 2017-10-24 00:10:32 -0400 | [diff] [blame] | 54 | mutate( |
| 55 | Species = cell_spec(Species, color = spec_color(1:10, option = "A"), link = "#", |
| 56 | tooltip = paste0("Sepal Length: ", Sepal.Length)) |
| 57 | ) %>% |
Hao Zhu | 457acb4 | 2017-10-14 17:37:02 -0400 | [diff] [blame] | 58 | mutate_if(is.numeric, function(x){ |
Hao Zhu | 6290fdd | 2017-10-24 00:10:32 -0400 | [diff] [blame] | 59 | cell_spec(x, "html", color = spec_color(x), font_size = spec_font_size(x), bold = T) |
Hao Zhu | 457acb4 | 2017-10-14 17:37:02 -0400 | [diff] [blame] | 60 | }) %>% |
Hao Zhu | 6290fdd | 2017-10-24 00:10:32 -0400 | [diff] [blame] | 61 | kable("html", escape = F, align = "c") %>% |
Hao Zhu | f6be00b | 2017-10-14 19:14:42 -0400 | [diff] [blame] | 62 | kable_styling("condensed", full_width = F) |
Hao Zhu | 457acb4 | 2017-10-14 17:37:02 -0400 | [diff] [blame] | 63 | ``` |
Hao Zhu | 064990d | 2017-10-17 18:08:42 -0400 | [diff] [blame] | 64 | |