## 2009-01-17

### Use of include and input in Sweave documents

When you write a large structured document using LaTeX, it is wise to use \input{} and/or \include{} commands (see here for a nice description). However, you can not "fully" use these two commands with Sweave documents. You can use them, but when you weave the master file, the \input{} and \include{} files are not weaved! The author of Sweave instead implemented the \SweaveInput{} command. If you still want to use \input{} and/or \include{}, then you can take the following approach.

Say we have a master file file0.Rnw, which includes file1.Rnw and file2.Rnw. Then you need first to run Sweave on file1.Rnw and file2.Rnw and at the end on file0.Rnw. Warning! - there can be no interaction between files, which basically means that you can use this approach only if each file represents a complete analysis. This might be a suitable Makefile in such cases:
`all: sweave # Compile the whole document Sweave.sh --latex2pdf file0.Rnwsweave: # Sweave individual files Sweave.sh file1.Rnw file2.Rnw`
If master file is a pure LaTeX file, you can change the all target to:
`all: sweave # Compile the whole document Sweave.sh --noweave --latex2pdf file0.tex`

Mark Pearson said...

Is there a way to use \SweaveInput with LyX? When I do:

\SweaveInput{myfile.R}

I get errors which I think stem from the fact that myfile.R isn't in the temporary LyX directory that LyX sets up for compilation, because if I put the file in that directory, it works. It doesn't work with full filepaths either. Is there a way to get LyX to handle this properly and automagically as it does with \inputted pdf or jpeg etc. files?

Thank you,
Mark

Gregor Gorjanc said...

Mark, this is a known issue[1] and some solutions were proposed (read the thread at [1]), but it will probably take some time to polish this. The problem lies in tha fact that LyX moves a file to a temporary directory during the compilation, i.e., from /home/me/test.lyx to say /tmp/test.lyx. However, a full path should work! Can you show an example of a full path that does not work?