Knitr 的 Pandoc 转换失败

Pandoc Conversion Failure With Knitr

我认为我的问题很简单,无需 MRE 即可解决。这是抛出错误的代码:

DSP %>%
kable(format = "latex",
        digits = 2,
        booktabs = T,
        format.args = list(big.mark = ',')) %>%
  kable_styling(font_size = 9,latex_options = c("striped", "scale_down")) %>%
  column_spec(1, bold = TRUE) %>%
  column_spec(10, bold = TRUE) %>%
  row_spec(nrow(DSP), bold = T)

我在一个RMarkdown文件中运行这个,我在一个FOR循环中执行,i = 50。换句话说,数据帧DSP得到re-generated 50次,并且每次都有不同的行数。 DSP 总是有 10 列。

我想将最后一行加粗。

对于 i = 1,nrows = 14。没问题。对于 i=2,nrows=10。没问题。对于 i=3,nrows = 9。我得到这个:

/Applications/RStudio.app/Contents/MacOS/pandoc/pandoc +RTS -K512m -RTS statement_merge.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash-implicit_figures --output pandoc1246b13bd105f.pdf --template /Users/steve-guest/Library/R/3.4/library/rmarkdown/rmd/latex/default-1.17.0.2.tex --highlight-style tango --latex-engine pdflatex --variable graphics=yes --variable 'geometry:margin=1in' 
! Package array Error:  Illegal pream-token (N): `c' used.

See the array package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.123 \end{tabular}}

pandoc: Error producing PDF
Error: pandoc document conversion failed with error 43
In addition: Warning messages:
1: Removed 7 rows containing missing values (position_stack). 
2: Removed 33 rows containing missing values (position_stack). 
3: Removed 7 rows containing missing values (position_stack). 
4: Removed 22 rows containing missing values (position_stack). 
5: Removed 5 rows containing missing values (position_stack). 
6: Removed 8 rows containing missing values (position_stack). 

如果我随后将 nrow(DSP) 替换为数字 9 并调用 render() 语句,它仍然会失败,但会出现不同的错误消息:

output file: statement_merge.knit.md

/Applications/RStudio.app/Contents/MacOS/pandoc/pandoc +RTS -K512m -RTS statement_merge.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash-implicit_figures --output pandoc1246b295881e6.pdf --template /Users/steve-guest/Library/R/3.4/library/rmarkdown/rmd/latex/default-1.17.0.2.tex --highlight-style tango --latex-engine pdflatex --variable graphics=yes --variable 'geometry:margin=1in' 
! Package array Error:  Illegal pream-token (N): `c' used.

See the array package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.123 \end{tabular}}

pandoc: Error producing PDF
Error: pandoc document conversion failed with error 43
In addition: Warning messages:
1: Removed 5 rows containing missing values (position_stack). 
2: Removed 8 rows containing missing values (position_stack). 

有人知道为什么会这样吗?更好的是,我该如何解决它?

谢谢!

编辑:pandoc headers

---
output: 
  pdf_document:
    fig_caption: false
header-includes:
  - \usepackage{booktabs}
  - \usepackage{longtable}
  - \usepackage{array}
  - \usepackage{multirow}
  - \usepackage[table]{xcolor}
  - \usepackage{wrapfig}
  - \usepackage{float}
  - \usepackage{colortbl}
  - \usepackage{pdflscape}
  - \usepackage{tabu}
  - \usepackage{threeparttable}

  - \definecolor{ufogrn}{rgb}{.2,.3,.1}

---

我认为尝试分解您的管道并确定问题的来源是一种很好的做法。我遇到了同样的问题,就我而言,它来自 kableExtra::column_spec() 当我不小心格式化了一个不存在的列。

当您收到来自 LaTex 的错误消息时,这表明格式出错了,而且它的位置并不总是很直观。您的管道从数据集开始,并通过每个步骤中的 kablekableExtra 函数向数据添加一些乳胶标记。如果您确定是哪一步导致了问题,您通常可以很容易地找到原因。