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

D G Rossiter rossiter at itc.nl
Thu Jun 28 03:06:04 CEST 2007


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

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



More information about the R-help mailing list