0

我认为我的问题很简单,可以在没有 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 重新生成 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 标头

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

---
4

1 回答 1

4

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

当您从 LaTex 收到错误消息时,这表明格式出现问题,并且在哪里并不总是很直观。您的管道从数据集开始,并通过每个步骤中的kableandkableExtra函数向数据添加一些乳胶标记。如果您确定导致问题的步骤,通常可以轻松找到原因。

于 2018-05-31T13:08:20.733 回答