title: another LaTeX Error: Missing \begin{document} in rmarkdown
body: <p>I'm automating a pdf report using rmarkdown.  I use a macro to run the code.  I can run the code once and it works with no problems. When I call the macro again, it appears to work but when creating a pdf, I get the error "LaTeX Error: Missing \begin{document}" </p>
  
  <p>This is what I get the first time:</p>
  
  <pre><code>output file: L:/Statunit/morton/NCC R markdown reports/NCC Reports/NCC_Dashboard_Report_Dave.knit.md
  
  "C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS "L:/Statunit/morton/NCC R markdown reports/NCC Reports/NCC_Dashboard_Report_Dave.utf8.md" --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output pandoc9e03c3032cf.tex --template "C:\Users\Mortond\Documents\R\win-library\3.5\rmarkdown\rmd\latex\default-1.17.0.2.tex" --highlight-style tango --latex-engine xelatex --variable graphics=yes --variable "geometry:margin=1in" --variable "compact-title:yes" --include-in-header "C:\Users\Mortond\AppData\Local\Temp\Rtmp8cWvvQ\rmarkdown-str9e022b75c22.html" 
  
  Output created: Report-254-225573.pdf
  </code></pre>
  
  <p>The second time, I call the same code but only change the report name, so the data is the same and I get.</p>
  
  <pre><code>output file: L:/Statunit/morton/NCC R markdown reports/NCC Reports/NCC_Dashboard_Report_Dave.knit.md
  
  "C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS "L:/Statunit/morton/NCC R markdown reports/NCC Reports/NCC_Dashboard_Report_Dave.utf8.md" --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output pandoc9e01f0a74c5.tex --template "C:\Users\Mortond\Documents\R\win-library\3.5\rmarkdown\rmd\latex\default-1.17.0.2.tex" --highlight-style tango --latex-engine xelatex --variable graphics=yes --variable "geometry:margin=1in" --variable "compact-title:yes" 
  ! LaTeX Error: Missing \begin{document}.
  
  Error: Failed to compile Report-253-225573.tex. See Report-253-225573.log for more info.
  </code></pre>
  
  <p>my YAML is</p>
  
  <pre><code>---
      title: ''
      header-includes: 
      - \usepackage{fancyhdr}
      - \addtolength{\headheight}{1.0cm} % make more space for the header
      - \pagestyle{fancyplain} % use fancy for all pages except chapter start
      - \lhead{\includegraphics[height=1.2cm]{TJC_logo_color.png}} % left logo
      - \renewcommand{\headrulewidth}{0pt} % remove rule below header
      output:
         pdf_document: 
         latex_engine: xelatex
         word_document: default
         html_document: default
         urlcolor: blue
         classoption: landscape
  ---
  
  my code that calls the markdown is :
      render_report = function(b,h,p) {
        rmarkdown::render(
         "L:/Statunit/morton/NCC R markdown reports/NCC Dashboard Report 
            Dave.Rmd", params = list(
            b1 = b,
            h1 = h,
           p1 = p
          ),
          output_file = paste0("Report-", h, "-", p, ".pdf")
        )
      }
  
      render_report(b="xxxx Hospital, Inc.",h='253',p='225573')
  </code></pre>
  
  <p>The log file with the error part is.</p>
  
  <pre><code>("C:\Users\Mortond\AppData\Local\Programs\MiKTeX 2.9\tex\latex\graphics-def\xet
  ex.def"
  File: xetex.def 2017/06/24 v5.0h Graphics/color driver for xetex
  ))
  \Gin@req@height=\dimen160
  \Gin@req@width=\dimen161
  )
  ("C:\Users\Mortond\AppData\Local\Programs\MiKTeX 2.9\tex\latex\oberdiek\grffile
  .sty"
  Package: grffile 2017/06/30 v1.18 Extended file name support for graphics (HO)
  Package grffile Info: Option `multidot' is set to `true'.
  Package grffile Info: Option `extendedchars' is set to `false'.
  Package grffile Info: Option `space' is set to `true'.
  Package grffile Info: \Gin@ii of package `graphicx' fixed on input line 494.
  )
  ("C:\Users\Mortond\AppData\Local\Programs\MiKTeX 2.9\tex\latex\parskip\parskip.
  sty"
  Package: parskip 2018-08-24 v2.0a non-zero parskip adjustments
  )
  ("C:\Users\Mortond\AppData\Local\Programs\MiKTeX 2.9\tex\latex\titling\titling.
  sty"
  Package: titling 2009/09/04 v2.1d maketitle typesetting
  \thanksmarkwidth=\skip53
  \thanksmargin=\skip54
  \droptitle=\skip55
  )
  ("C:\Users\Mortond\AppData\Local\Programs\MiKTeX 2.9\tex\latex\fancyhdr\fancyhd
  r.sty"
  Package: fancyhdr 2017/06/30 v3.9a Extensive control of page headers and footer
  s
  \f@nch@headwidth=\skip56
  \f@nch@O@elh=\skip57
  \f@nch@O@erh=\skip58
  \f@nch@O@olh=\skip59
  \f@nch@O@orh=\skip60
  \f@nch@O@elf=\skip61
  \f@nch@O@erf=\skip62
  \f@nch@O@olf=\skip63
  \f@nch@O@orf=\skip64
  )
  
  ! LaTeX Error: Missing \begin{document}.
  
  See the LaTeX manual or LaTeX Companion for explanation.
  Type  H &lt;return&gt;  for immediate help.
   ...                                              
  
  l.90 \addtolength{\headheight}{1.0cm} \%
                                           make more space for the header 
  Here is how much of TeX's memory you used:
   22493 strings out of 427767
   408844 string characters out of 3146884
   530389 words of memory out of 3000000
   26423 multiletter control sequences out of 15000+200000
   532722 words of font info for 28 fonts, out of 3000000 for 9000
   1328 hyphenation exceptions out of 8191
   45i,0n,68p,816b,443s stack positions out of 5000i,500n,10000p,200000b,50000s
  
  No pages of output.
  </code></pre>
  
  <p>So why does it work once and not a second time?  If I exit RStudio and then s start it up again, it appears to work.  I've tried to<br>
      <code>.rs.restartR()</code>    to no avail. as well as 
      <code>rm(list = ls(envir = globalenv()),envir = globalenv())</code>
      <code>gc()</code> to clean things up.  </p>
  
  <p>Any thoughts?  I appreciate reading through all this.</p>