2

I have an R analysis that I want to present in an R markdown table. The table's layout demands some empty cells:

x <- c(1:14)

Header1 | Header2 | Header3  | Header4
------- | ------- | -------- | -------
Label1  | LabelA  | `r x[1]` | `r x[4]`
Label2  | LabelB  | `r x[2]` | `r x[5]`
        | LabelC  | `r x[3]` | `r x[6]`
        | LabelD  |          | `r x[7]`
------- | ------- | -------- | -------                    
Label3  | LabelA  | `r x[8]` | `r x[11]`
Label4  | LabelB  | `r x[9]` | `r x[12]`
        | LabelC  | `r x[10]`| `r x[13]`
        | LabelD  |          | `r x[14]`

However, those empty cells are causing the layout to break, ie, LabelC appears directly under Label2 even though it is supposed to be one column over.

I know, from searching on this issue, that you can't merge table cells in R Markdown. But can I fill those cells with code such that they will present as empty? I tried filling them with r NULL but nothing happened.

mmyoung77
  • 1,343
  • 3
  • 14
  • 22

1 Answers1

4

The problem with this table is that it does not have the style required by rmarkdown. The table below has the good style and there's no issue with this table:

| Header1 | Header2 | Header3  | Header4   |
|:-------:|:-------:|:--------:|:---------:|
| Label1  | LabelA  | `r x[1]` | `r x[4]`  |
| Label2  | LabelB  | `r x[2]` | `r x[5]`  |
|         | LabelC  | `r x[3]` | `r x[6]`  |
|         | LabelD  |          | `r x[7]`  |
| ------- | ------- | -------- | --------- |                    
| Label3  | LabelA  | `r x[8]` | `r x[11]` |
| Label4  | LabelB  | `r x[9]` | `r x[12]` |
|         | LabelC  | `r x[10]`| `r x[13]` |
|         | LabelD  |          | `r x[14]` |

You can generate such a table with the pander package, function pandoc.table with the option style="rmarkdown", or with knitr::kable. Both have an option for dealing with the missing values.

Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225