[R] Sweave bug? when writing figures / deleting variable in chunk
Achim Zeileis
Achim.Zeileis at wu-wien.ac.at
Thu Jun 28 03:34:58 CEST 2007
On Wed, 27 Jun 2007 21:06:04 -0400 D G Rossiter wrote:
> I have found a quite strange (to me) behaviour in Sweave. It only
> occurs in the following situation:
>
> 1. define a variable in one chunk
> 2. use it within a subsequent figure-generating chunk
> 3. delete it at the end of that same chunk
> Then the Sweave driver chokes, not finding the variable name when
> generating the figure
Sweave() executes figure chunks more than once because they might
also create printed output. If you create both EPS and PDF graphics, it
executes them three times (print + EPS + PDF). Hence, data
manipulations should be avoided in figure chunks.
I was also once bitten by this when I permuted columns of a table prior
to plotting and never obtained the desired order...
Fritz, maybe this is worth an entry in the FAQ?
Z
> Example:
>
> % document bug2.Rnw
> \documentclass{article}
> \usepackage{Sweave}
> \begin{document}
> \SweaveOpts{eps=false}
> <<>>=
> sel <- 1:5
> @
> <<fig=T>>=
> plot(trees[sel,])
> rm(sel)
> @
> \end{document}
>
> Try to sweave:
>
> > Sweave("bug2.Rnw")
> Writing to file bug2.tex
> Processing code chunks ...
> 1 : echo term verbatim
> 2 : echo term verbatim pdf
> Error: no function to return from, jumping to top level
> Error in plot(trees[sel, ]) : error in evaluating the argument 'x'
> in selecting a method for function 'plot'
> Error in driver$runcode(drobj, chunk, chunkopts) :
> Error in plot(trees[sel, ]) : error in evaluating the
> argument 'x' in selecting a method for function 'plot'
>
> The generated .tex is complete up through the rm() but no figure is
> generated. The file bug2-002.pdf is incomplete (corrupt).
>
> ...
> \begin{Schunk}
> \begin{Sinput}
> > plot(trees[sel, ])
> > rm(sel)
> \end{Sinput}
> \end{Schunk}
>
> The following ALL eliminate the problem:
>
> 0. Executing the code directly, also with ESS
> 1. <<fig=F>>
> 2. moving rm(sel) to a separate, later code chunk
> 3. Stangle the source and then source it
> 4. don't use a variable, i.e. in this case: plot(trees[1:5,])
>
> It seems that Sweave is executing the rm(sel) before it uses it in
> the trees[sel,].
>
> Technical details: R 2.5.0, Mac OS X 10.4.10, PPC
> Same behaviour in stand-alone R for Mac and for R within Aquamacs
> using ESS
>
> Workaround: I am putting any deletions into a later code chunk. This
> only has the disadvantage of making more chunks, so now that I know
> what's happening it's no big deal. But it's driving me crazy... am I
> missing something? Thanks!
>
> D. G. Rossiter
> Senior University Lecturer
> Department of Earth Systems Analysis
> International Institute for Geo-Information Science and Earth
> Observation (ITC)
> Hengelosestraat 99
> PO Box 6, 7500 AA Enschede, The Netherlands
> Phone: +31-(0)53 4874 499
> Fax: +31-(0)53 4874 336
> mailto:rossiter--at--itc.nl, Internet: http://www.itc.nl/personal/
> rossiter
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html and provide commented,
> minimal, self-contained, reproducible code.
>
More information about the R-help
mailing list