Missing \begin{document} in R Markdown to PDF

Hello All,
This is my first time on a forum...! :slight_smile: I am running a dos batch file to generate pdfs via rmarkdown::render. Here is my code - it is for 2 markdowns of 2 R notebooks with some graphical output. The first markdown works fine and generates a pdf as it should. The second (similar) call fails on the pandoc call with ! LaTeX Error: Missing \begin{document}. The end of this output is shown after the DOS below. Bizarrely, if I hang on to the intermediate file structure with the graphics with clean=FALSE and then run the tex file (which is seemingly complete) from Tex Live it works fine. Going mad with this, have looked evrywhere...any ideas please?
Thank you
Steve

THE DOS FILE

cd ..
rem
rem Replace the single back slashes with double for R input
rem
set WORKING_DIREC=%cd:=\%

rem -----------------------------------------------
rem Run an R notebook (markdown) program
rem -----------------------------------------------

set RProg_to_open=%WORKING_DIREC%\R Programs\Notebooks\MetricCoverageReport.Rmd
rem
rem Chop off the \Data Processing Software part of the pathname stub (26 characters) before bolting on the output data filename
rem because latex does NOT like to see \..\ and an error occurs! It took ages to find this.
rem
set WORKING_DIREC=%WORKING_DIREC:~0,-26%
set pdf_output=%WORKING_DIREC%\Output Data\MetricCoverageReport.pdf
rem
rem IMPORTANT the 32 bit i386 version must be run to give compatibility with 32 bit ODBC drivers!!
rem
"C:\Program Files\R\R-3.6.2\bin\i386\Rscript.exe " -e "library(markdown);Sys.setenv(RSTUDIO_PANDOC='C:\Program Files\RStudio\bin\pandoc');rmarkdown::render('%RProg_to_open%',output_file='%pdf_output%')"
rem
rem Now the values
rem
set WORKING_DIREC=%cd:=\%

set RProg_to_open=%WORKING_DIREC%\R Programs\Notebooks\MetricValuesReport.Rmd
rem
rem Chop off the \Data Processing Software part of the pathname stub (26 characters) before bolting on the output data filename
rem because latex does NOT like to see \..\ and an error occurs! It took ages to find this.
rem
set WORKING_DIREC=%WORKING_DIREC:~0,-26%
set pdf_output=%WORKING_DIREC%\Output Data\MetricValuesReport.pdf
rem
rem IMPORTANT the 32 bit i386 version must be run to give compatibility with 32 bit ODBC drivers!!
rem
"C:\Program Files\R\R-3.6.2\bin\i386\Rscript.exe " -e "library(markdown);Sys.setenv(RSTUDIO_PANDOC='C:\Program Files\RStudio\bin\pandoc');rmarkdown::render('%RProg_to_open%',output_file='%pdf_output%',clean=FALSE)"
pause

SOME OF THE MARKDOWN OUTPUT (First line shows end of first markdown working successfully)

Output created: D:\woodlandtrust_fwwq\Fingle Woods Water Quality Project\Output Data\MetricCoverageReport.pdf
Warning message:
package 'lubridate' was built under R version 3.6.3

processing file: MetricValuesReport.Rmd
|....... | 10%
ordinary text without R code

|.............. | 20%
label: unnamed-chunk-1 (with options)
List of 1
$ include: logi FALSE

|..................... | 30%
label: unnamed-chunk-2 (with options)
List of 2
message: logi FALSE warning: logi FALSE

Loading required package: lattice

Attaching package: 'lubridate'

The following object is masked from 'package:base':

date

|............................ | 40%
inline R code fragments

|................................... | 50%
label: unnamed-chunk-3 (with options)
List of 2
fig.width : num 8 fig.height: num 8

|.......................................... | 60%
label: unnamed-chunk-4 (with options)
List of 2
fig.width : num 8 fig.height: num 8

|................................................. | 70%
label: unnamed-chunk-5 (with options)
List of 2
fig.width : num 8 fig.height: num 8

|........................................................ | 80%
label: unnamed-chunk-6 (with options)
List of 2
fig.width : num 8 fig.height: num 8

|............................................................... | 90%
label: unnamed-chunk-7 (with options)
List of 2
fig.width : num 8 fig.height: num 8

|......................................................................| 100%
ordinary text without R code

output file: MetricValuesReport.knit.md

"C:/PROGRA~1/Pandoc/pandoc" +RTS -K512m -RTS MetricValuesReport.utf8.md --to latex --from markdown+autolink_bare_uris+tex_math_single_backslash --output pandoc23d48b565b9.tex --self-contained --highlight-style tango --pdf-engine pdflatex --variable graphics --lua-filter "C:/Users/Steve Creamer/Documents/R/win-library/3.6/rmarkdown/rmd/lua/pagebreak.lua" --lua-filter "C:/Users/Steve Creamer/Documents/R/win-library/3.6/rmarkdown/rmd/lua/latex-div.lua" --variable "geometry:margin=1in"
! LaTeX Error: Missing \begin{document}.

Error: LaTeX failed to compile D:\woodlandtrust_fwwq\Fingle Woods Water Quality Project\Output Data\MetricValuesReport.tex. See The R package tinytex - Helper Functions to Manage TinyTeX, and Compile LaTeX Documents - Yihui Xie | 谢益辉 for debugging tips. See MetricValuesReport.log for more info.
In addition: Warning messages:
1: package 'lubridate' was built under R version 3.6.3
2: package 'suncalc' was built under R version 3.6.3
Execution haltedPlease ask your questions about R Markdown here.

Hello, its me replying to myself. To cut to the chase I realised that by renaming the .Rmd and the .pdf file names I could knit interactively or in batch without a hitch! I then deleted any of the R workspace files that I had in any folder and reran with the original filename and guess what, it works! I would never have guessed really, just getting desperate! So now I can create the PDFs from batch really easily. If its any use to anybody the dos code is shown below. Just modify for your own filepaths. Can't believe that I wrote my thesis in this stuff in the early 80s and its still going! Mind you, I was using FORTRAN too...... :rofl:

cd ..
rem
rem Replace the single back slashes with double for R input
rem

set WORKING_DIREC=%cd:=\%

rem -----------------------------------------------
rem Run an R notebook (markdown) program
rem -----------------------------------------------

set RProg_to_open=%WORKING_DIREC%\R Programs\Notebooks\MetricCoverageReport.Rmd
rem
rem Chop off the \Data Processing Software part of the pathname stub (26 characters) before bolting on the output data filename
rem because latex does NOT like to see \..\ and an error occurs! It took ages to find this.
rem
set WORKING_DIREC=%WORKING_DIREC:~0,-26%
set pdf_output=%WORKING_DIREC%\Output Data\MetricCoverageReport.pdf
rem
rem IMPORTANT the 32 bit i386 version must be run to give compatibility with 32 bit ODBC drivers!!
rem
"C:\Program Files\R\R-3.6.2\bin\i386\Rscript.exe " -e "library(markdown);Sys.setenv(RSTUDIO_PANDOC='C:\Program Files\RStudio\bin\pandoc');rmarkdown::render('%RProg_to_open%',output_file='%pdf_output%')"

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.