I want to group column names in an RMarkdown table for the Word/docx output.
Using pandoc.table
:
library(pander)
pandoc.table(mtcars[1:3, 1:4], style = "rmarkdown")
| | mpg | cyl | disp | hp |
|:-------------------:|:-----:|:-----:|:------:|:----:|
| **Mazda RX4** | 21 | 6 | 160 | 110 |
| **Mazda RX4 Wag** | 21 | 6 | 160 | 110 |
| **Datsun 710** | 22.8 | 4 | 108 | 93 |
This produces the output below, which is fine
But, say I want to group mpg
& cyl
in one group, and disp
& hp
in another like so (modified by hand):
| | group1 | group2 |
|:-------------------:|:-------------:|:-------------:|
| | mpg | cyl | disp | hp |
|:-------------------:|:-----:|:-----:|:------:|:----:|
| **Mazda RX4** | 21 | 6 | 160 | 110 |
| **Mazda RX4 Wag** | 21 | 6 | 160 | 110 |
| **Datsun 710** | 22.8 | 4 | 108 | 93 |
This won't work and the output looks like this:
If possible, I would also like to add the title in a merged top cell, like so (obviously this also does not work):
|**Table 1.** This is a table title ..........xxxxxx |
|possibly wrapped to the next line. |
|:---------------------------------------------------:|
| | group1 | group2 |
|:-------------------:|:-------------:|:-------------:|
| | mpg | cyl | disp | hp |
|:-------------------:|:-----:|:-----:|:------:|:----:|
| **Mazda RX4** | 21 | 6 | 160 | 110 |
| **Mazda RX4 Wag** | 21 | 6 | 160 | 110 |
| **Datsun 710** | 22.8 | 4 | 108 | 93 |