在Rstudio中将rmarkdown文件编译为PDF文档时,R中的kableExtra函数返回乳胶错误

时间:2018-11-22 15:46:06

标签: r r-markdown kableextra

我正在尝试将rmarkdown文件编织为包含 kableExtra 功能的PDF文件,例如:

  • kable_styling
  • column_spec
  • add_header_above

但是我总是遇到以下错误:

! Extra alignment tab has been changed to \cr.
<template> \endtemplate 

l.184 \end{tabular}}


pandoc.exe: Error producing PDF
Error: pandoc document conversion failed with error 43
Ejecución interrumpida

我正在执行的一个简单示例是:

```
---
output:
 pdf_document:
   keep_tex: yes
classoption: table
header-includes:
  - \usepackage{array}
  - \usepackage{float}
  - \usepackage{xcolor}
---

```{r results='asis'}
options(kableExtra.latex.load_packages = FALSE)
require(kableExtra)
print(kable(head(cars),"latex")%>%kable_styling(latex_options = c("striped", 
"bordered"))
  %>%column_spec(column=1:2,width = "0.5in") %>%
      kable_styling(c("striped", "bordered"),latex_options = 
"scale_down")%>% add_header_above(c(" "=7,
"Absolute"=1,"Relative"=1,"Absolute"=1,"Relative"=1,
"Absolute"=1,"Relative"=1,"Absolute"=1,"Relative"=1,"Absolute"=1,
"Relative"=1))%>%
add_header_above(c(" "= 1,"Non-weighted"=1,"Weighted"=1,"Non- 
weighted"=1,"Weighted"=1,"Non-weighted"=1,
"Weighted"=1,"Weighted"=2,"Non-weighted"=2,"Weighted"=2,"Non- 
weighted"=2,"Weighted"=2))%>%
add_header_above(c("Theoretical Values"= 1,"First-Order Predicted 
Value"=2,"Second-Order Predicted Value"=2,
"Third-Order Predicted Value"=2,
"Non-linearity 1st Order"=2,"Non-linearity 2nd Order"=4,"Non-linearity 3rd 
Order"=4)))

```

有人可以帮助我解决这个问题吗?

任何线索我都会感谢!

1 个答案:

答案 0 :(得分:1)

您要告诉kableExtra不要加载LaTeX软件包(为什么?),因此您必须自己执行此操作。您使用的功能需要arrayfloatxcolor以及table选项。一个困难是fancyvrb v3.0已经用other options加载了xcolor。您可以通过提供table作为类选项来避免这种情况:

---
output: 
  pdf_document:
    keep_tex: yes
classoption: table    
header-includes:
  - \usepackage{array}
  - \usepackage{float}
---


```{r results='asis'}
options(kableExtra.latex.load_packages = FALSE)
require(kableExtra)
print(kable(head(cars),"latex") %>%column_spec(column=1:2,width = "0.5in"))
```

```{r results='asis'} 
print(kable(head(cars),"latex")%>%kable_styling(latex_options = c("striped", "bordered")))
```