blob: 9823a7cff079ac4e9d93a18c4a4c5cd4793e4ecf [file] [log] [blame]
Hao Zhu37975a32019-01-15 14:59:34 -06001---
2title: "Use kableExtra and xml2 in the HARD way"
3author: "Hao"
4date: "1/15/2019"
5output: html_document
6---
7
8```{r setup, include=FALSE}
9knitr::opts_chunk$set(echo = TRUE)
10```
11
12Behind the scene, for HTML tables, `kableExtra` uses `xml2` to add styles and new contents to existing table. `kableExtra` is trying its best to get you the result in a literal way but in some rare case, I hope you can have the ability to freedom to be a HTML Ninja (quoting @yihui's xaringan). :)
13
14In `kableExtra` 1.0, I exported two functions that were previously used internally in this package: `kable_as_xml` and `xml_as_kable`. Here is an example for how to use these 2 function and xml2 to hack your table and do something `kableExtra` is not capable to do (at least right now).
15
Hao Zhu84102ed2019-01-16 00:06:25 -060016<style>
17@-webkit-keyframes rotate {
18 0% {transform: rotate(0deg)}
19 100% {transform: rotate(360deg)}
20}
21
22.badIdea {
23 position:relative;
24 animation: rotate 3s linear infinite;
25}
26</style>
27
Hao Zhu37975a32019-01-15 14:59:34 -060028```{r}
29library(kableExtra)
30library(xml2)
31
Hao Zhu84102ed2019-01-16 00:06:25 -060032demo <- kable(mtcars[1:4, 1:4]) %>% # As always, mtcars ;)
Hao Zhu37975a32019-01-15 14:59:34 -060033 kable_styling(full_width = F) %>%
34 kable_as_xml()
35
36demo %>% # Transform to xml
37 xml_child(2) %>% # Select <tbody> (1 is <thead>)
38 xml_child(2) %>% # Select 2nd row <tr> in <tbody>
39 xml_child(1) %>% # Select 1st cell in 2nd row <td>
Hao Zhu84102ed2019-01-16 00:06:25 -060040 xml_set_attr("class", "badIdea") # Add attribute to style
Hao Zhu37975a32019-01-15 14:59:34 -060041
42xml_as_kable(demo) # Render that xml back as kable
43```
44
Hao Zhu84102ed2019-01-16 00:06:25 -060045> Only for illustration. Animating your texts like this is a **horrible** idea and will distract audience's attention.
46> I mean seriously, what I want to show here is `xml2`...
47
48
Hao Zhu37975a32019-01-15 14:59:34 -060049For those `%>%` fans, keep in mind that since `xml2` makes modification to an external xml object. Although it looks like you can put everything in one pipe, the reality is that you will have to save the entire xml first, run the `xml2` code and then get it rendered.
Hao Zhu84102ed2019-01-16 00:06:25 -060050
51***
52
53Note:
54Here is my simple rotation css. You need to put them in rmarkdown to get things work.
55```{css, eval = F}
56@-webkit-keyframes rotate {
57 0% {transform: rotate(0deg)}
58 100% {transform: rotate(360deg)}
59}
60
61.badIdea {
62 position:relative;
63 animation: rotate 3s linear infinite;
64}
65```