0

I think my problem is straightforward enough that it can be answered without a 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. DSP always has 10 columns.

I want to bold the last row.

For i = 1, nrows = 14. No problem. For i=2, nrows= 10. No problem. For i=3, nrows = 9. I get this:

/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:

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

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

---
Steve
  • 575
  • 4
  • 18

1 Answers1

4

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.

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. 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. If you identify which step causes the problem, you can usually find the cause easily.

Daniel Antal
  • 226
  • 2
  • 9
  • Thanks Daniel. It took me a while, but I finally figured out the solution -- by doing what you recommended. (I wish I'd read your answer first!) – Steve Jun 02 '18 at 00:37