# [R] calling sweave function from latex

Frank E Harrell Jr fharrell at virginia.edu
Wed Jan 29 18:18:03 CET 2003

On Wed, 29 Jan 2003 11:16:11 +0100
Friedrich.Leisch at ci.tuwien.ac.at wrote:

> >>>>> On Wed, 29 Jan 2003 14:12:15 +1300,
> >>>>> Sam McClatchie (SM) wrote:
>
>   > System info:
>   > Mandrake 9.0
>   > R Version 1.6.1
>   > ESS 5.1.21
>   > Emacs 21.2.1
>   > -------------------
>
>   > Colleagues
>
>   > I've been calling R-code embedded in my LaTex document using Sweave, but
>   > would like to make things more convenient. At present as I understand it
>   >   you first process the R chunks of code using the Sweave function
>   > called from within R to process a "precursor file" e.g. foo.sw to get a
>   > LaTex file (foo.sw.tex) that you then process with latex foo.sw.tex.
>   > ------------------------
>   > example code segment
>
>   > %\item {\bf Matched trawl and acoustic data} \label{real data}
>
>   > \item {\bf Results}
>
>   > %%%% sweave code
>
>   > <<echo=false,results=hide>>=
>   > average.trawl.spp.composition()
>   > @
>
>   > %%%% insert figure generated from sweave code
>   > \begin{figure}
>   > \includegraphics[scale=0.6]{../figures/bycatch_by_weight}
>   > \caption{\label{catch by weight} Proportions of selected species (from
>   >      Table \ref{ts length regressions}) in the fish assemblage using
>   >      catch rate ($kg\ km{-1}$) as an approximation for fish density
>   >      (neglecting variable capture efficiencies). Note: there were no
>   >      oblique banded rattails in this dataset, although we have a
>   >      \textit{<TS>-length} regression for them (see Table \ref{ts length
>   >      regressions}). Box plot centre line = meadian, box limits are
>   >      $25^{th}$ and $75^{th}$ quartiles, whiskers represent 1.5 times the
>   >      interquartile range from the median, and points outside the whiskers
>   >      are the tails of the distributions.}
>   > \end{figure}
>   > -----------------------
>
>   > This works fine, but it is cumbersome for someone who likes to write a
>   > bit and then latex that additional bit. Of course I can just add the new
>   > LaTex code chunks to the foo.sw.tex and latex that, but I have to
>   > remember to copy the foo.sw.tex back to foo.sw or the versions get mixed
>   > up. Trivial, but annoying.
>
>   > The question is: can I call the Sweave function from within LaTex so I
>   > just latex the foo.sw.tex and the Sweave chunks will also get processed.
>   > This would be much tidier.
>
>   > One suspects that the short answer is 'no'.
>
>
>
> One way to deal with this is to write several Sweave filew and collect
> them into the main latex document using \input{} statements. Then you
> can Sweave() only those files which have some changes, makefiles can
> automate that easily.
>
> I have some ideas on conditional processing of chunks and re-using
> previously saved results in case nothing has changed, but that has not
> been implemented yet.
>
> Best,
> Fritz
>

Before I discovered Sweave, I used methods that are detailed in Chapter 13 of http://hesweb1.med.virginia.edu/biostat/s/doc/splus.pdf for managing large analysis projects.  The do function in the Hmisc library (not yet tested for R) was created to manage code chunks that each create their own series of output listings and postscript graphics files.  For conditional execution of chunks, it helps me to set flags at the start of the document so that I don't have to hunt for any flags or if statements.  I do things like

importdata    <- FALSE
fitmodel      <- FALSE
validatemodel <- TRUE

if(importdata) {
...
}
if(fitmodel) {
...
}
if(validatemodel) {
... time-consuming bootstrap code ...
}

or use

do(importdata, { ... code chunk ...})
do(fitmodel, {... code chunk ...})
do(validatemodel, {... code chunk ...})

where the keys such as 'importdata' are used as root names of files created by that code chunk.  The 'validatemodel' chunk creates by default 'validatemodel.lst' to contain the printed output, and files such as 'validatemodel.ps' for graphics.

It would be nice if Sweave could implement this type of model.  In LaTeX I find it invaluable to set flags for conditional text insertion using \def and \ifnum \fi.  For a report, the text might always be included but S chunks only condionally run, with previous output used when a chunk is bypassed.

Please take a look at Chapter 13 of the above referenced document if you have time, to see if it gives you any ideas worth pursuing.

Sincerely,

Frank
--
Frank E Harrell Jr              Prof. of Biostatistics & Statistics
Div. of Biostatistics & Epidem. Dept. of Health Evaluation Sciences
U. Virginia School of Medicine  http://hesweb1.med.virginia.edu/biostat



More information about the R-help mailing list