在非折叠列中使用带有非唯一值的kableExtra collapse_rows时出错

时间:2017-11-29 22:28:33

标签: r r-markdown kableextra kable

我使用Rmarkdown创建包含表格并使用kableExtra::collapse_rows的PDF。当我的非折叠列重复出现值时,我在编织时遇到错误。

---
output: 
  pdf_document:
    latex_engine: xelatex
---

```{r}
library(magrittr)
library(knitr) 
library(kableExtra) 

# Build data with repeated values in last column
dat1 <- data.frame(x = c(rep("First",times=4),rep("Second",times=3)),
                   y = c(rep(4,times=4),rep(3,times=3)),
                   z = c("det1","det2","det2","det3","det4","det5","det5"),
                   stringsAsFactors=FALSE)

# This works fine:
dat1 %>%
  kable(format="latex", booktabs = TRUE, row.names=FALSE)

enter image description here

# Throws an error when I try to collapse rows of first two columns:
dat1 %>%
  kable(format="latex", booktabs = TRUE, row.names=FALSE) %>%
  collapse_rows(columns = 1:2)

我得到的错误信息是:

  

!额外对齐选项卡已更改为\ cr。

     

<recently read> \ endtemplate

     

l.169 First&amp; \ vphantom {1} 4&amp; det2&amp;

     

pandoc.exe:生成PDF错误

     

错误:pandoc文档转换失败,错误43

# Change up the data so last column has all unique values:
dat2 <- dat1
dat2$z <- c("det1","det2","det3","det4","det5","det6","det7")

# Works fine:
dat2 %>%
  kable(format="latex", booktabs = TRUE, row.names=FALSE) %>%   
  collapse_rows(columns = 1:2)
```

enter image description here

我工作的情况要求允许重复最后一列中的项目,因此我希望能够生成一个不错的版本,前两列的行已折叠。我不确定我错过了什么。

0 个答案:

没有答案