8

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.

mdsumner
  • 29,099
  • 6
  • 83
  • 91
Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
  • off-topic, but on the off-chance this is helpful: http://staff.acecrc.org.au/~mdsumner/TCallaghan/ That template shows how to use the include/includeonly infrastructure, and has all the pieces at least for one institution. I used it directly, and have just replaced chap1.tex etc. with my Rnw equivalents and added Sweave to the pre-latex process. – mdsumner Aug 11 '10 at 00:15
  • I edited the title to say how I understood your question. Feel free to edit again, if I understood it wrong. (I still didn't really understand the problem and how the accepted answer helped, though.) – Paŭlo Ebermann Aug 27 '11 at 20:30
  • The problem was that at least two of the files were Sweave files and that the results from one weren't able to be seen by the other. I think the original title was simpler and clearer, though informal. Roman's still active here, so maybe he'll jump in, but in the meantime I'll put it back closer to how it started. Paŭlo, I hope this isn't rude, but why edit a question you don't understand? – Aaron left Stack Overflow Aug 28 '11 at 02:43
  • @Aaron, no offense taken. I think "help me" is in general a bad type of title, and [I'm now going through all questions of some tags with some of these bad key words](http://meta.stackexchange.com/questions/101923/ive-found-a-particular-set-of-questions-with-bad-titles-how-should-i-proceed/102197#102197) trying to give them better titles. (My tag here was [tag:latex], not [tag:r] or [tag:sweave]). You are right, I should have simply commented in this case. – Paŭlo Ebermann Aug 28 '11 at 10:34
  • Paŭlo, thanks for not taking offense and providing a great answer to my question. Your work to improve the site is appreciated. – Aaron left Stack Overflow Aug 28 '11 at 15:56

5 Answers5

5

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

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

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.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
2

To expand Dirk's and mjm's answer, I would suggest using \include's and Makefiles.

Suppose you have a master file: master.tex. In that file, you include some .tex and .Rnw files, i.e.

\include chapter1
\include chapter2
\include chapter3
....

Now the following Makefile provides functions for creating the .tex, .R and .pdf files:

.SUFFIXES: .tex .pdf .Rnw .R

MAIN = master
##List your your .Rnw includes
RNWINCLUDES = chapter1 chapter2 chapter3
TEX = $(RNWINCLUDES:=.tex)
RFILES = $(RNWINCLUDES:=.R)
RNWFILES = $(INCLUDES:=.Rnw)

all: $(MAIN).pdf
    $(MAIN).pdf: $(TEX) $(MAIN).tex

R: $(RFILES)

.Rnw.R:
     R CMD Stangle $<

.Rnw.tex:
     R CMD Sweave $<

.tex.pdf:
     pdflatex $<
     bibtex $*
     pdflatex $<
     pdflatex $<

Essentially, the .SUFFIXES provide a set of rules for convert from one file format to another. For example, to convert from .Rnw to .R, we use the command

`R CMD Stangle $<`
csgillespie
  • 59,189
  • 14
  • 150
  • 185
1

one fairly obvious answer is to use a makefile, possibly using package cachesweave, to process the relevant files in the right order.

malecki
  • 556
  • 4
  • 4
0

My solution to multi-file projects in Sweave (under Rstudio) is the following:

1) Create a master file, say master.Rnw, in which you have the calls to the subfiles intro.Rnw, matmet.Rnw, etc:

\documentclass[11pt]{book}
% \usepackage{blah, blah} as you wish

\graphicspath{ {./figs/}

\begin{document}
\SweaveOpts{concordance=TRUE}

\include{intro} % a call to 'intro.Rnw'
\include{matmet} % a call to 'matmet.Rnw'
\include{results} % a call to 'results.Rnw'
\include{discuss} % a call to 'discuss.Rnw'

\end{document}

2) Create the subfiles. I'm giving here only the first one, intro.Rnw. Please note that in the subfiles you do not use preambular commands such as \documentclass or \begin{document}

\chapter{Introduction}\label{ch:intro}
\section{This is section 01}
In section 01 we are concerned about whether \texttt{Sexpr} could possibly work. The chunk below creates a variable \em{a} which will be referred to by this command later on.

<<>>=
a <- 1+2
@

Ok, if it is working, we shall see number 3 right here: \Sexpr{a}.

3) After saving modifications in 'intro.Rnw', simply go to 'master.Rnw' and compile it using Ctrl+Shift+K and... voilá:

Screenshot of the file created by the above command.

Og DeSouza
  • 103
  • 1
  • 7