5

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

---
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)

在此处输入图像描述

# 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 &\vphantom{1} 4 & det2 &

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

在此处输入图像描述

我正在处理的情况要求允许最后一列中的项目重复,所以我希望能够生成一个很好的版本,前两列的行折叠起来。我不确定我错过了什么。

4

0 回答 0