Search code examples
rloopsparent-childknitrsweave

Stop knitr preamble in looped child .Rnw documents


I am looping a child .Rnw file so I can repeat plots with different species. Unfortunately the preamble for knitr (I think) is being repeated at the beginning of every loop so the .pdf will not compile. If I manually remove the repeated preamble in the .tex file everything is fine. I have a feeling I am missing some link between parent and child but cannot find where exactly. I am using R-studio and the files outlined below.

main.Rnw:

\documentclass{article}
\begin{document}

<<>>=
some R stuff
@

<<runall, include =FALSE>>=
out<-c()
for(species in c('RH', "GU", "RA", "KI")){
out<- c(out, knit_child('spsummary.Rnw')
}
@

\Sexpr{paste(out, collapse='n')}
\end{document}

The child document (spsummary.Rnw) is looped over each species:

<<>>=
set_parent("main.Rnw")
@

<<>>=
more R stuff
@

\begin{table}
...
\end{table}

\clearpage
\newpage

Solution

  • I finally answered my own question.

    Its a simple case of a missing backslash.

    the last but one line of the parent document (main) should read:

    \Sexpr{paste(out, collapse='\n')}
    

    with a backslash before the 'n'