Why does rendering a pdf from rmarkdown require closing rstudio between renders? : <h2>Background</h2>

<p>I am trying to make a rmarkdown document that is rendered using <code>render()</code>. The <code>render</code> call has two elements that are parameterized:</p>

<ul>
<li>I would like the user to be able to specify pdf or html.
Straightforward using <code>output_format()</code>.</li>
<li>I also would like to pass a parameter to the document to specify if the tables (using the <code>kableExtra</code> package) are latex or html. </li>
</ul>

<p>This is the rmarkdown file called test.Rmd</p>

<pre><code>---
title: "Title"
author: "Zzz Zzzzzz"
params:
  table_format:
    value
---

```{r setup}
knitr::opts_chunk$set(echo = FALSE)
library(knitr)
library(kableExtra)

options(knitr.table.format = params$table_format) 
```

## Test table

```{r cars}
if (params$table_format == "latex"){
kable(iris[1:100,], booktabs = T) %&gt;%
  kable_styling(latex_options = c("scale_down")) 
}

if (params$table_format == "html"){
kable(iris[1:100,]) %&gt;%
  kable_styling(bootstrap_options = c("striped", "hover"))  %&gt;%
  scroll_box(width = "500px", height = "600px")
}


params$table_format
```
</code></pre>

<p>Now here are the two calls to render the file:</p>

<pre><code>rmarkdown::render("test.Rmd", output_format = "pdf_document", params = list(
  table_format = "latex"
))


rmarkdown::render("test.Rmd", output_format = "html_document", params = list(
  table_format = "html"
))
</code></pre>

<h2>Problem</h2>

<p>Now if I open up a fresh rstudio session I can run both <code>render</code> calls no problem. Either .pdf or an .html file is created. However if I then try to run the .pdf render <em>again</em> i get the following error:</p>

<blockquote>
  <p>"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS
  test.utf8.md --to latex --from
  markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash
  --output test.pdf --template "C:\Users\salbers\R\win-library\3.4\rmarkdown\rmd\latex\default-1.17.0.2.tex"
  --highlight-style tango --latex-engine pdflatex --variable graphics=yes --variable "geometry:margin=1in"  ! Undefined control
  sequence.  \begin {tabular}{rrrrl} \toprule 
                                              Sepal.Length &amp; Sepal.Width &amp; Pet... l.267 \end{tabular}}</p>
  
  <p>pandoc.exe: Error producing PDF Error: pandoc document conversion
  failed with error 43 In addition: Warning message: running command
  '"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS
  test.utf8.md --to latex --from
  markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash
  --output test.pdf --template "C:\Users\salbers\R\win-library\3.4\rmarkdown\rmd\latex\default-1.17.0.2.tex"
  --highlight-style tango --latex-engine pdflatex --variable graphics=yes --variable "geometry:margin=1in"' had status 43</p>
</blockquote>

<p>The html render works fine over and over again. If I close rstudio, then ropen the project, the render for pdf work fine as well.</p>

<h2>Question</h2>

<ul>
<li>Can anyone tell me why my pdf rendered rmarkdown document can't be rendered twice in a single session of rstudio? </li>
<li>Similarly, why does rstudio have to closed in between renderings?</li>
</ul>
