Search code examples
rlatexsweave

Writing big documents with Sweave. Is it possible to do as with LaTeX?


I am just discovering Sweave and R. I have seen some examples of Sweave documents and have also started to write one or two on my own. I am impressed by the ability of doing computations in R and outputting results directly in a LaTeX document.

Now I am thinking of bigger documents (as we usually have with LaTeX) that consist of several pages and several parts. With LaTeX (I use WinEdt), I set a main document (e.g. main.tex) and then have other subsidiary documents like introduction.tex, discussion.tex etc.

My question is: Can we do this with Sweave as well? Now I am working with single Sweave document (.Rnw) alone. Can we have multiple Sweave documents (with one main and the secondary ones) like we normally do with LaTeX?

A workaround would be to have separate Sweave files and then sweave them to produce the R LaTeX chunks which can be copied to a LaTeX document but then the whole idea seems quite inefficient and time consuming.

Please do let know what suggestions and solutions that you have.

Thanks a lot...


Solution

  • Here is what works very well for me:

    I have one master file ("master.Rnw") that has no text but only serves to collect the files (chapters, sections) which form the document in the end.

    Then I have one file with R code that is being reused in various other files ("func.Rnw"). Here, I have lot of named chunks

    <<my_fun_1,eval=FALSE,echo=FALSE>>=
    # code:
    c <- a * b
    @
    

    In master.Rnw, the first thing after \begin{document} I do is

    \SweaveInput{func.Rnw}
    

    and from there, I have my named chunks available. In a file "chap1.Rnw" I can now have

    <<echo=FALSE>>=
    a <- 10
    b <- 25
    <<my_fun_1>>
    c
    @
    

    Of course, I have to

    \SweaveInput{chap1.Rnw})
    

    into master.Rnw.

    I only have to \Sweave{master.Rnw} and then pdflatex the resulting master.tex file, no copying/ pasting or processing of multiple files.

    I'm just writing a paper of 60+ pages with about 25 tables and figures and everything works perfectly well so far.

    Hope this helps, Rainer