简体   繁体   English

用Knitr进行Pandoc转换失败

[英]Pandoc Conversion Failure With Knitr

I think my problem is straightforward enough that it can be answered without a MRE. 我认为我的问题很简单,无需MRE就可以解决。 Here's the code that's throwing an error: 这是引发错误的代码:

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)

I'm running this in an RMarkdown file, which I execute in a FOR loop, with i = 50. In other words, the data frame DSP gets re-generated 50 times, and each time it has a different number of rows. 我在RMarkdown文件中运行此文件,该文件在FOR循环中以i = 50执行。换句话说,数据帧DSP会重新生成50次,每次具有不同的行数。 DSP always has 10 columns. DSP始终有10列。

I want to bold the last row. 我想加粗最后一行。

For i = 1, nrows = 14. No problem. 对于i = 1,行数=14。没问题。 For i=2, nrows= 10. No problem. 对于i = 2,nrows =10。没问题。 For i=3, nrows = 9. I get this: 对于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). 

If I then replace nrow(DSP) with the number 9 and call the render() statement, it still fails, but with a different error message: 如果我然后将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). 

Does anyone know why this is happening? 有人知道为什么会这样吗? And even better, how I can fix it? 甚至更好,我该如何解决?

Thanks! 谢谢!

EDIT: pandoc headers 编辑: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}

---

I think it is a good practice to try to break down your pipeline and identify where the problem comes from. 我认为这是一个很好的做法,尝试破坏您的渠道并确定问题出处。 I had the same issue, and in my case it came from kableExtra::column_spec() when I accidentally formatted a non-existing column. 我遇到了同样的问题,在我的情况下,当我不小心格式化了一个不存在的列时,它来自kableExtra::column_spec()

When you get the error message from LaTex that is a sign that something went wrong in formatting, and it is not always very intuitive where. 当您从LaTex收到错误消息时,这表明格式出现问题,而且并非总是很直观。 Your pipeline is starting from a data set, and via the kable and kableExtra functions in each step it add some latex markups to the data. 您的管道从数据集开始,并通过kablekableExtra函数在每个步骤中向数据添加一些乳胶标记。 If you identify which step causes the problem, you can usually find the cause easily. 如果确定导致问题的步骤,通常可以轻松找到原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM