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 |
| 6 | --- |
| 7 | |
| 8 | 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. |
| 9 | |
| 10 | ```{r, message=FALSE, warning=FALSE} |
| 11 | library(knitr) |
| 12 | library(kableExtra) |
| 13 | library(formattable) |
| 14 | library(dplyr) |
| 15 | ``` |
| 16 | |
| 17 | ```{r} |
| 18 | mtcars[1:5, 1:4] %>% |
| 19 | mutate( |
| 20 | car = row.names(.), |
| 21 | mpg = color_tile("white", "orange")(mpg), |
| 22 | cyl = cell_spec(cyl, "html", angle = (1:5)*60, |
| 23 | background = "red", color = "white", align = "center"), |
| 24 | disp = ifelse(disp > 200, |
| 25 | cell_spec(disp, "html", color = "red", bold = T), |
| 26 | cell_spec(disp, "html", color = "green", italic = T)), |
| 27 | hp = color_bar("lightgreen")(hp) |
| 28 | ) %>% |
| 29 | select(car, everything()) %>% |
| 30 | kable("html", escape = F) %>% |
| 31 | kable_styling("hover", full_width = F) %>% |
| 32 | column_spec(5, width = "3cm") %>% |
| 33 | add_header_above(c(" ", "Hello" = 2, "World" = 2)) |
| 34 | ``` |
| 35 | |
| 36 | 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). |
| 37 | |
| 38 | Also, if you are using it in this way, make sure you put `escape = F` in `kable`. |
| 39 | |
| 40 | 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. |
Hao Zhu | 457acb4 | 2017-10-14 17:37:02 -0400 | [diff] [blame] | 41 | |
| 42 | *** |
| 43 | |
| 44 | I also added a few helper functions to use together with cell_spec. One good example is `spec_color`, which gives you the ability to use viridis color map in your table. Others include `spec_font_size` and `spec_angle`. |
| 45 | |
| 46 | ```{r} |
| 47 | iris[1:10, ] %>% |
| 48 | mutate_if(is.numeric, function(x){ |
Hao Zhu | f6be00b | 2017-10-14 19:14:42 -0400 | [diff] [blame] | 49 | cell_spec(x, "html", color = spec_color(x), bold = T) |
Hao Zhu | 457acb4 | 2017-10-14 17:37:02 -0400 | [diff] [blame] | 50 | }) %>% |
Hao Zhu | ce5ee41 | 2017-10-23 01:14:38 -0400 | [diff] [blame^] | 51 | mutate(Species = cell_spec(Species, background = "red")) %>% |
| 52 | kable("html", col.names = c("A", "B", "C", "D", "DDDDDDDDDDD"), escape = F, align = "c") %>% |
Hao Zhu | f6be00b | 2017-10-14 19:14:42 -0400 | [diff] [blame] | 53 | kable_styling("condensed", full_width = F) |
Hao Zhu | 457acb4 | 2017-10-14 17:37:02 -0400 | [diff] [blame] | 54 | ``` |
Hao Zhu | 064990d | 2017-10-17 18:08:42 -0400 | [diff] [blame] | 55 | |