knitr
has a few pretty straightforward ways of handling this.
Option 1: Using knit_child()
with inline R code
Say your setup is like the following. In the same directory, you have:
graph.R
## ---- graph
library(ggplot2)
CarPlot <- ggplot() +
stat_summary(data= mtcars,
aes(x = factor(gear),
y = mpg
),
fun.y = "mean",
geom = "bar"
)
CarPlot
chapter1.Rnw
Hey, look, a graph (Figure~\ref{fig:graph})!
<<graph, echo=FALSE, message=FALSE, fig.lp='fig:', out.width='.5\\linewidth', fig.align='center', fig.cap="A graph", fig.pos='h!'>>=
@
main.Rnw
\documentclass{article}
\begin{document}
<<external-code, echo=FALSE, cache=FALSE>>=
read_chunk('./graph.R')
@
\Sexpr{knit_child('chapter1.Rnw')}
\end{document}
Then, you can knit
the main.Rnw
file and compile the resulting .tex
file with either pdflatex
or xelatex
.
The output is:
Note that you can also read the external .R
file from the child .Rnw
file.
So, the following would have worked just as well.
chapter1-mod.Rnw
<<external-code, echo=FALSE, cache=FALSE>>=
read_chunk('./graph.R')
@
Hey, look, a graph (Figure~\ref{fig:graph})!
<<graph, echo=FALSE, message=FALSE, fig.lp='fig:', out.width='.5\\linewidth', fig.align='center', fig.cap="A graph", fig.pos='h!'>>=
@
main-mod.Rnw
\documentclass{article}
\begin{document}
\Sexpr{knit_child('chapter1-mod.Rnw')}
\end{document}
Option 2: Using chunk option child
Assuming you have graph.R
and chapter1.Rnw
from above in the same directory, then your main.Rnw
should be:
\documentclass{article}
\begin{document}
<<external-code, echo=FALSE, cache=FALSE>>=
read_chunk('./graph.R')
@
<<child-demo, child='chapter1.Rnw'>>=
@
\end{document}
Note that you can also read the external .R
file from within the child document in this case, too.
So, assuming you had graph.R
and chapter1-mod.Rnw
from above in the same directory, then your main-mod.Rnw
file should be:
\documentclass{article}
\begin{document}
<<child-demo, child='chapter1-mod.Rnw'>>=
@
\end{document}
The easiest way to do it, would be to make your main document also a .Rnw
file,
that could contain nothing but LaTeX (at first).
Within the main .Rnw
document you could do something like
<<child="myTable.Rnw">>=
@
where myTable.Rnw
can be any .Rnw
file that does not include header information, e.g. your example document without documentclass
and begin{document}
/\end{document}
Of course you could just as well include the r-chunk from myTable.Rnw
in to the main document (as well as other r-chunks).
Best Answer
Here is an example using the knitr child file process as per the knitr documentation yihui.name/knitr/demo/child.
First the new main file which I called 'knitr01.Rnw'
Note that I inputed the child twice each with a different dataset.
And the child file which I named 'child-knitr01.Rnw'.
When run first through 'knit' and then through 'pdflatex' it results in
To continue the demonstration for completeness, this also allows child files to input grandchildren.
The knitr01.Rnw is changed as follows.
Here is the revised 'child-knitr01.Rnw' file
And here is the 'grand-child-knitr01.Rnw' file
And the output is: