Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

r markdown kable break table width into multiple tables below each other

Is there a way to tell kable to break a long table (that would exceed the text or page width) into multiple ones that are shown below each other?

I mean, you can do this like this:

```{r}
data <- data.frame(....)
kable(data[,1:6])
kable(data[,7:12])
```

But this is a lot of work in figuring out where to break the data.frame. Is there a way to do it automatically? I found no option in the kable function itself.

One way to do it computationally is to calculate the string width of the longest entries in the table (and the header) and break the table depending on this. But I still hope there is already a good way to do it...

Optimally I would like to do this for html page generation, but a way to do it for latex would also be great.

like image 433
nnn Avatar asked Jan 15 '16 14:01

nnn


1 Answers

pander can do this for you and many more -- quick example on the split table feature with directly specified function arguments (see ?pandoc.table for more details, which is called by the general pander S3 method in the background):

> pander(head(mtcars), split.table = 80, style = 'rmarkdown')

|         &nbsp;          |  mpg  |  cyl  |  disp  |  hp  |  drat  |
|:-----------------------:|:-----:|:-----:|:------:|:----:|:------:|
|      **Mazda RX4**      |  21   |   6   |  160   | 110  |  3.9   |
|    **Mazda RX4 Wag**    |  21   |   6   |  160   | 110  |  3.9   |
|     **Datsun 710**      | 22.8  |   4   |  108   |  93  |  3.85  |
|   **Hornet 4 Drive**    | 21.4  |   6   |  258   | 110  |  3.08  |
|  **Hornet Sportabout**  | 18.7  |   8   |  360   | 175  |  3.15  |
|       **Valiant**       | 18.1  |   6   |  225   | 105  |  2.76  |

Table: Table continues below

|         &nbsp;          |  wt   |  qsec  |  vs  |  am  |  gear  |  carb  |
|:-----------------------:|:-----:|:------:|:----:|:----:|:------:|:------:|
|      **Mazda RX4**      | 2.62  | 16.46  |  0   |  1   |   4    |   4    |
|    **Mazda RX4 Wag**    | 2.875 | 17.02  |  0   |  1   |   4    |   4    |
|     **Datsun 710**      | 2.32  | 18.61  |  1   |  1   |   4    |   1    |
|   **Hornet 4 Drive**    | 3.215 | 19.44  |  1   |  0   |   3    |   1    |
|  **Hornet Sportabout**  | 3.44  | 17.02  |  0   |  0   |   3    |   2    |
|       **Valiant**       | 3.46  | 20.22  |  1   |  0   |   3    |   1    |

You can set a global threshold for the number of characters via global options among many other things, eg:

> panderOptions('table.split.table', 80)
> panderOptions('table.style', 'grid')
> panderOptions('table.split.cells', 10)
> panderOptions('table.alignment.default', 'left')
> panderOptions('table.alignment.rownames', 'right')
> panderOptions('decimal.mark', ',')
> pander(head(mtcars))

+----------------+-------+-------+--------+------+--------+-------+
|         &nbsp; | mpg   | cyl   | disp   | hp   | drat   | wt    |
+================+=======+=======+========+======+========+=======+
|        **Mazda | 21    | 6     | 160    | 110  | 3,9    | 2,62  |
|          RX4** |       |       |        |      |        |       |
+----------------+-------+-------+--------+------+--------+-------+
|        **Mazda | 21    | 6     | 160    | 110  | 3,9    | 2,875 |
|      RX4 Wag** |       |       |        |      |        |       |
+----------------+-------+-------+--------+------+--------+-------+
|       **Datsun | 22,8  | 4     | 108    | 93   | 3,85   | 2,32  |
|          710** |       |       |        |      |        |       |
+----------------+-------+-------+--------+------+--------+-------+
|     **Hornet 4 | 21,4  | 6     | 258    | 110  | 3,08   | 3,215 |
|        Drive** |       |       |        |      |        |       |
+----------------+-------+-------+--------+------+--------+-------+
|       **Hornet | 18,7  | 8     | 360    | 175  | 3,15   | 3,44  |
|   Sportabout** |       |       |        |      |        |       |
+----------------+-------+-------+--------+------+--------+-------+
|    **Valiant** | 18,1  | 6     | 225    | 105  | 2,76   | 3,46  |
+----------------+-------+-------+--------+------+--------+-------+

Table: Table continues below

+----------------+--------+------+------+--------+--------+
|         &nbsp; | qsec   | vs   | am   | gear   | carb   |
+================+========+======+======+========+========+
|        **Mazda | 16,46  | 0    | 1    | 4      | 4      |
|          RX4** |        |      |      |        |        |
+----------------+--------+------+------+--------+--------+
|        **Mazda | 17,02  | 0    | 1    | 4      | 4      |
|      RX4 Wag** |        |      |      |        |        |
+----------------+--------+------+------+--------+--------+
|       **Datsun | 18,61  | 1    | 1    | 4      | 1      |
|          710** |        |      |      |        |        |
+----------------+--------+------+------+--------+--------+
|     **Hornet 4 | 19,44  | 1    | 0    | 3      | 1      |
|        Drive** |        |      |      |        |        |
+----------------+--------+------+------+--------+--------+
|       **Hornet | 17,02  | 0    | 0    | 3      | 2      |
|   Sportabout** |        |      |      |        |        |
+----------------+--------+------+------+--------+--------+
|    **Valiant** | 20,22  | 1    | 0    | 3      | 1      |
+----------------+--------+------+------+--------+--------+
like image 157
daroczig Avatar answered Oct 23 '22 14:10

daroczig