24

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...

yCalleecharan
  • 4,656
  • 11
  • 56
  • 86

3 Answers3

19

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

vaettchen
  • 7,299
  • 22
  • 41
  • @ vaettchen Thanks a lot for the detailed explanation. If I understand well then one does not need to explicitly set a master.Rnw file in an R editor (I do not know if this is even possible). One just have to use put `Sweaveinput{...}` in the master file. Interesting. I have to try your concept. 1 vote up for now. – yCalleecharan Dec 03 '11 at 08:34
  • @ yCalleecharan: Beware - it is \SweaveInput{} with upper case I – vaettchen Dec 03 '11 at 08:45
  • @ vaettchen . Ok I have to ask you some details. In master.Rnw we have: \documentclass[a4paper]{article} and \usepackage{Sweave} and after \begin{document}, we put \SweaveInput{func.Rnw} and \SweaveInput{chap1.Rnw}). Secondly in chap1.Rnw we have JUST R codes delimited by <<>>== and @ and here you use your functions as you show in your example here to output c. Is that alright? And finally in the file func.Rnw we have also JUST R code with the function my_fun1 that you gave. So is it only in master.Rnw that we set \document{class} option and \usepackage{Sweave} option? – yCalleecharan Dec 03 '11 at 09:08
  • @ yCalleecharan: Correct, only master.Rnw needs \documentclass{} and \usepackage{} and stuff, all other files are simple text files that contain Sweave chunks and LaTeX formatted text. After running `\Sweave{master.Rnw}` you will have only one master.tex file with the content of all files and all necessary information for successful LaTeX compilation. – vaettchen Dec 03 '11 at 09:29
  • @ vaettchen Unfortunately for me I get: Error in SweaveReadFile(c(ifile, file), syntax, encoding = encoding) : no Sweave file with name './chap1.Rnw)' found Calls: Sweave -> SweaveReadFile -> SweaveReadFile Execution halted – yCalleecharan Dec 03 '11 at 09:36
  • @ yCalleecharan: You have all three files in the same directory, and no typing errors? `\SweaveInput{func.Rnw}` works but `\SweaveInput{chap1.Rnw}` doesn't? – vaettchen Dec 03 '11 at 09:43
  • @ vaettchen. Ok It works fine now! I was switching between WinEdt and R-Tinn, pasting here and there and in the process I accidentally put an additional brace with \SweaveInput{chap1.Rnw}. It was my mistake. Thank you very much again for this ELEGANT solution. – yCalleecharan Dec 03 '11 at 09:50
4

I'm using Sweave to write a book with about 16 chapters. I agree that using a makefile with multiple Rnw files is a good idea. One other nice thing abut this approach is that make can be run in parallel (-p I think), so depending on how the chapter's objects depend on each other, you can run a lot of code simultaneously.

The thing that has made a difference for me is caching. Several of my code chunks can run for days but have not changed over the course of writing the book. There are a few packages that allow you to save the results when it is run and only re-run the chunk if the objects that the chunk depends on have changed.

There are a few packages to do this. See:

http://cran.r-project.org/web/views/ReproducibleResearch.html

I use the weaver package form Bioconductor.

While I'm on a brain dump... if you have more than one author, I found that a shared Dropbox account is an excellent way of maintaining the project.

Max

topepo
  • 13,534
  • 3
  • 39
  • 52
1

I recommend using RStudio (http://www.rstudio.com/ide/) instead of WinEdt. Sweave is nicely integrated into that IDE and, as in WinEdt you can set one file as the master file. From the master file you can include child files using

\SweaveInput{Child.Rnw}

You can link a child file back to the master file by including the directive

% !Rnw root = Master.Rnw

in the child file. That way when working on a child file and typesetting it, RStudio know to typeset the master file.

The details are explained in the RStudio documentation at http://www.rstudio.com/ide/docs/authoring/multiple_rnw_files

The nicest feature is that Synctex and TeX error log navigation still work even when working with multi-file documents.

Gustav Delius
  • 1,933
  • 1
  • 16
  • 10