ERROR: "Extra alignment tab has been changed to \cr." in R-Markdown to PDF

ERROR: "Extra alignment tab has been changed to \cr." in R-Markdown to PDF

我正在使用带有 KableExtra 的 Rmarkdown 来创建带有 collapse_rows 和 pack_rows 的发票 table。在本地执行此块会生成所需的 table 布局:

example table layout

一旦我尝试使用以下方式呈现此降价:rmarkdown::render() 我收到以下错误:

output file: billing.knit.md

/usr/bin/pandoc +RTS -K512m -RTS billing.knit.md --to latex --from markdown+autolink_bare_uris+tex_math_single_backslash --output pandoc6d77420b47ac.tex --self-contained --highlight-style tango --latex-engine pdflatex --variable graphics --include-in-header /tmp/RtmpoyiqX9/rmarkdown-str6d777c393955.html
! Extra alignment tab has been changed to \cr.
<recently read> \endtemplate

我只能通过跳过 collapse_rows & pack_rows 来防止这个错误。

kbl(tot_cost3[,2:ncol(tot_cost3)], format = "latex", booktabs = TRUE, longtable = TRUE, col.names = c("", "analysis type", "samples", "unit price", "sum", "sub total in CHF"), align = "lllcccc") %>%
  kable_styling(latex_options = c("repeat_header")) %>%
  collapse_rows(columns = 6, valign = "bottom", latex_hline = "major", longtable_clean_cut = T) %>%
  pack_rows(index = table(tot_cost$User_name)) %>%
  row_spec(nrow(tot_cost3), bold=TRUE) %>%
  kable_paper(full_width = T)

我真的不明白这里发生了什么,因为回滚到 *.Rmd 的早期工作版本也无济于事。 - 它似乎仍然与 pack_rowscollapse_rows 函数有某种关系...

非常感谢任何关于潜在解决方案的提示!

我已经找出导致问题的原因:

“tot_cost3”的其中一个单元格中存在换行符 \n,导致 table 格式在使用 [=22 时中断=] 或 collapse_rows!

LaTeX, somehow shows surprisingly high tolerance on that, which is quite unusual. As a result, it won’t throw an error if you are just using kable to make some simple tables. However, when you use kableExtra to make some advanced modification, it will start to throw some bugs

来自 Hao Zhou 的“使用 knitr::kable 和 kableExtra 创建很棒的 LaTeX Table”(http://haozhu233.github.io/kableExtra/awesome_table_in_pdf.pdf) 教程为我提供了在 [=24= 中查找格式问题的提示] 内容本身...