如何处理 R markdown 中的空单元格? [英] How to handle empty cells in R markdown?

查看:80
本文介绍了如何处理 R markdown 中的空单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 R 分析,我想在 R 降价表中展示.表格的布局需要一些空单元格:

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]`

然而,那些空单元格导致布局中断,即 LabelC 直接出现在 Label2 下,即使它应该超过一列.

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.

我知道,通过搜索此问题,您无法在 R Markdown 中合并表格单元格.但是我可以用代码填充这些单元格,使它们显示为空吗?我尝试用 r NULL 填充它们,但什么也没发生.

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.

推荐答案

这个表的问题是它没有 rmarkdown 要求的样式.下表风格不错,这张表没有问题:

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]` |

您可以使用 pander 包、函数 pandoc.table 和选项 style="rmarkdown" 或使用knitr::kable.两者都有处理缺失值的选项.

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.

这篇关于如何处理 R markdown 中的空单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆