Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I Sweave a multiple-file project?

I am writing my thesis in LaTeX and because things got a bit long for my taste, I had split it into several files. Let's call them thesis.tex, intro.tex, mat_n_met.tex, rslts.tex and discsn.tex. I have linked intro.tex, mat_n_met.tex, rslts.tex and discsn.tex through thesis.tex with \include{intro} (and so on...). I have also created a separate file called r_crunching.Rnw (that I run through Sweave) that holds a chunk that runs the R script with data analysis and chunks that produce pdf outputs of graphs that I embed via \includegraphics (in e.g., rslts.tex). Still following?

If I run a Rnw (i.e. I renamed rslts.tex to rslts.Rnw) without "a link" to the chunk with the R script, you will get a Sweave() error saying the reference in \Sexpr{} doesn't exist. Is there a way, without merging all the files into a single .Rnw, to call \Sexpr{} in say rslts.Rnw?

Other methods how to accomplish this are welcome.

like image 492
Roman Luštrik Avatar asked Aug 10 '10 18:08

Roman Luštrik


People also ask

How to use r Sweave?

To start a new Sweave document, go to File | New and select "R Sweave". This will provide a basic Sweave template. From here, you can enter text and LaTeX commands. R chunks can also be inserted to interweave R commands and output into your document.

What are Rnw files?

Rnw (or Sweave) files support authoring documents that contain a mix of content and R code, allowing you to execute and embed the results of R computations and graphics within a document. This enables the creation of dynamic reports that are updated automatically if data or analysis changes.


2 Answers

I recommend using RStudio (http://www.rstudio.com/ide/). Sweave is nicely integrated into that IDE and it supports multi-file documents. Even Synctex and TeX error log navigation still work when working with multi-file documents.

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

like image 66
Gustav Delius Avatar answered Sep 18 '22 17:09

Gustav Delius


Forget for a second that you are dealing with Sweave and just think of the latex problem -- for which \include and \includeonly offer solutions. Try that with a few simple test files.

Once you have that figured out, fold Sweave back into the mix and it just work as Sweave is after 'merely' a pre-processing step, albeit a very clever one.

like image 43
Dirk Eddelbuettel Avatar answered Sep 20 '22 17:09

Dirk Eddelbuettel