D G Rossiter
2007-Jun-28 01:06 UTC
[R] Sweave bug? when writing figures / deleting variable in chunk
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
Deepayan Sarkar
2007-Jun-28 01:31 UTC
[R] Sweave bug? when writing figures / deleting variable in chunk
On 6/27/07, D G Rossiter <rossiter at itc.nl> 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 figureThe reason is that by default, every fig=TRUE chunk is run twice, once to produce postscript, once for pdf. -Deepayan
Peter Dunn
2007-Jun-28 01:31 UTC
[R] Sweave bug? when writing figures / deleting variable in chunk
> I have found a quite strange (to me) behaviour in Sweave. It only > occurs in the following situation:You need to understand what Sweave does when it creates pictures:> <<>>> sel <- 1:5 > @ > <<fig=T>>> plot(trees[sel,]) > rm(sel) > @By default, a eps and pdf version of the graphic is made. That is, this chunk producing the graphic is *run twice*: once to make the eps file, once to make the pdf file. After this code chunk is run once:> <<fig=T>>> plot(trees[sel,]) > rm(sel)...the variable sel is obviously deleted, so the second time it runs... well, there's your error message. Best to place the command rm( sel ) in it's own separate chunk. P. -- Dr Peter Dunn | dunn <at> usq.edu.au Faculty of Sciences, USQ; http://www.sci.usq.edu.au/staff/dunn Aust. Centre for Sustainable Catchments: www.usq.edu.au/acsc This email (including any attached files) is confidential an...{{dropped}}
Duncan Murdoch
2007-Jun-28 01:34 UTC
[R] Sweave bug? when writing figures / deleting variable in chunk
On 27/06/2007 9:06 PM, 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 figureBy default, Sweave runs figure chunks twice (once for pdf, once for eps). They shouldn't change the environment they need to run in. Not sure where this is documented, but it's well known by people who've been bitten by it. Duncan Murdoch
Achim Zeileis
2007-Jun-28 01:34 UTC
[R] Sweave bug? when writing figures / deleting variable in chunk
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 figureSweave() 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. >