[R] Sweave bug? when writing figures / deleting variable in chunk

D G Rossiter rossiter at itc.nl
Thu Jun 28 14:14:44 CEST 2007


Thanks to the four distinguished R'ers who answered. I have used  
Sweave intensively for over a year, and have poured through all the  
doc. as well as the help archives, and could find nothing on this. It  
is now "well known" to me! since it bit me, but it did cost me a day  
of head-scratching and putting it in the shortest possible form for  
the list (the original example where this happened was a lot more  
complicated). Perhaps it could be documented in Sweave- 
manual-20060104.pdf or its successor?

Now I see the reasoning behind it, thanks. I had already determined  
the work-around. The problem is that I distribute the Tangled .R  
files along with the Sweaved PDF doc, so that students etc. can run  
the code. Anything unintuitive in the code confuses people. In this  
case I think it's good pratice to delete temporary variables right  
after they are no longer needed (as in my original example), so  
that's why I want to put it in the same chunk.

Having said that, I am a Sweave devotee, it has radically improved my  
productivity in writing R-based tech. notes.

David Rossiter
ITC Enschede (NL)

On 27 Jun2007, at 21:34, Achim Zeileis wrote:

> 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