[R] calling sweave function from latex

david.whiting@ncl.ac.uk david.whiting at ncl.ac.uk
Wed Jan 29 08:07:04 CET 2003

You say that you call R code embedded in your LaTeX document. I tend to
think of it as LaTeX embedded in my R code.  I think the answer to your
question is 'no', but there might be ways to work around it.

I use AUCTeX in emacs and can select a region of foo.sw and run latex on
that.  This works fine although you won't have access to R objects using
\Sexpr{}.  In practice I tend to use <<eval=FALSE>> to "comment out" R
code chunks that I have run previously if I do not need to update or
recreated their objects. This means that running Sweave on foo.sw then
does not take too long to run the R chunks that do need to be run, i.e.
foo.tex is created pretty quickly.  Running latex does not then usually
take long either.  I use search and replace or the chunk navigation
hotkeys (e.g. M-n g) when I need to change <<eval=FALSE>> to

Creating a Makefile to run R with Sweave (as indicated in the FAQ in the
Sweave manual) and then latex the resulting foo.tex should work pretty
well to automate the process. 

I am still new to Sweave, but have had a lot of fun with it so far.  I
use emacs with ESS, Sweave, RefTex mode, LaTeX mode and Prosper - a
presentation class that creates PDF presentations which (can if you
want) look like MS Powerpoint presentations but with the advantage of
LaTeX formatting.  All the tools I need for analysis, writing, and
presentations working together. 

My first serious use of this combination was last week. I had a foo.Rnw
file which produced both my presentation and accompanying notes to
handout. I discovered a mistake in my R code shortly before giving my
presentation, fixed it, Sweave'd the file and updated my presentation
and notes painlessly. A nice way to work.


On Wed, Jan 29, 2003 at 02:12:15PM +1300, Sam McClatchie 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'.
> Best fishes
> Sam
> -- 
> Sam McClatchie, Research scientist (fisheries acoustics))))))))))
> NIWA (National Institute of Water & Atmospheric Research Ltd)
> PO Box 14 901, Kilbirnie, Wellington, New Zealand
> s.mcclatchie at niwa.cri.nz
> Research home page <http://www.smcc.150m.com/>
>                       /\
>            >><xX(&>
>                    /// \\\
>                   //// \\\\
>                  ///  <%)Xx><<
>                 /////  \\\\\\
>           ><(((@>
>     ><(((%>     ..>><xX(?>O<?)Xx><<
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> http://www.stat.math.ethz.ch/mailman/listinfo/r-help

Dave Whiting
Dar es Salaam, Tanzania

More information about the R-help mailing list