I develop the R package vtable and someone recently reported this bug that I find very confusing regarding RMarkdown.
Specifically, when knitting to PDF, the first table I output is fine, but the second one isn't properly escaped. But only on its first line.
Here's an example:
---
title: "Untitled"
author: "Author"
date: "Date"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(vtable)
d <- data.frame(a = 1)
```
```{r b1, results = 'asis'}
vtable(d, out = 'latex')
```
```{r b2, results = 'asis'}
vtable(d, out = 'latex')
```
Note that if I leave out the b2 chunk, this builds to PDF fine. But with it in, the build fails because only the first table comes out fine. In the .tex output the code for it is
\begin{table}[!htbp] \centering
\renewcommand*{\arraystretch}{1.1}
\caption{Variable Table}
\begin{tabular}{p{0.290909090909091\textwidth}p{0.145454545454545\textwidth}p{0.363636363636364\textwidth}}
\hline
\hline
Name & Class & Values \\
\hline
a & numeric & Num: 1 to 1\\
\hline
\hline
\end{tabular}
\end{table}
But the second one is messed up, with some of the characters being improperly escaped and treated as text. Even weirder, only the first line is messed up. The LaTeX code for this table is the exact same as the previous code chunk, except that the first line is
\textbackslash begin\{table\}{[}!htbp{]} \centering
The rest works fine. If I add a third/fourth/etc. table they similarly have a messed up first line.
So what in RMarkdown is causing only the first line of the second and subsequent tables to be improperly escaped? And, any idea on how I can make the tables show up properly? Thank you!