# Bug in Sweave? -- scoping problem? (PR#8615)

4 messages
Open this post in threaded view
|

## Bug in Sweave? -- scoping problem? (PR#8615)

 I have found a strange scoping problem in Sweave.  The following Rnw file doesn't produce the same output in Sweave as it does if I produce an R file using Stangle and execute that: \documentclass[12pt]{article} \begin{document} <>= election <- data.frame(A=1:3, B=9:7, C=rep(0,3)) partytotal <- rep(0, ncol(election)) for (i in 1:ncol(election)) {       partytotal[i] <- sum(election[,i], na.rm=TRUE) } @ <>= names(partytotal) <- names(election) partytotal <- sort(partytotal) partytotal dotchart(partytotal) @ \end{document} The data.frame "election" contains columns which are vote counts for 3 parties, with rows being 3 electoral districts.   The column totals are 6 for A, 24 for B, and 0 for C.  I calculate those totals in a vector partytotal, assign the names to its entries, and then sort it. The strange thing is that while the value in partytotal is output correctly as \begin{Sinput}   > names(partytotal) <- names(election)   > partytotal <- sort(partytotal)   > partytotal \end{Sinput} \begin{Soutput}    C  A  B    0  6 24 \end{Soutput} \begin{Sinput}   > dotchart(partytotal) \end{Sinput} but the dotchart contains the wrong values:  it shows sorted values, but not sorted names, which is also what is left over in my workspace after the Sweave call:   > partytotal    A  B  C    0  6 24 These tests were run in R 2.2.1, but I see the same thing in today's R-devel. Duncan Murdoch ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Open this post in threaded view
|

## Re: Bug in Sweave? -- scoping problem? (PR#8615)

 G'day Duncan, >>>>> "DM" == murdoch  <[hidden email]> writes:     DM> I have found a strange scoping problem in Sweave.  [...]     DM> The strange thing is that while the value in partytotal is     DM> output correctly as [...]     DM> but the dotchart contains the wrong values: it shows sorted     DM> values, but not sorted names, [...] No bug, but a feature. :) (But then, a well known software producer seems to use feature' as a euphemism for bug', so perhaps I shouldn't call it a feature.) There was recently a discussion on r-help (?) about Sweave producing different output in the text and plot when random numbers were generated, and it seems as if you have run into the same trap:       Code in chunks that produce pictures is executed several times.       First, to produce the output in the text.  And then once more       for *each* format in which the figure has to be produced.  I.e.,       if you want a PDF and a PostScript version of the figure, the       code is executed a total of three times. All instances of this feature reported so far involved commands that produced random numbers and the poster was surprised that the output in the text and the figures differed (and that the two figures were different). In your case the first execution of the code assigns the names to partytotal, sorts partytotal and produces the data.  On the next execution, when the picture is produced, partytotal is already sorted but you reassign the names.  Then the partytotal is sorted again and the plot is produced.  But that re-assigning of names lead to the disconnect between values and names. Hope this helps. Cheers,         Berwin ========================== Full address ============================ Berwin A Turlach                      Tel.: +61 (8) 6488 3338 (secr)   School of Mathematics and Statistics        +61 (8) 6488 3383 (self)       The University of Western Australia   FAX : +61 (8) 6488 1028 35 Stirling Highway                   Crawley WA 6009                e-mail: [hidden email] Australia                        http://www.maths.uwa.edu.au/~berwin______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
 In reply to this post by Duncan Murdoch Berwin A Turlach wrote: > G'day Duncan, > > >>>>>>"DM" == murdoch  <[hidden email]> writes: > > >     DM> I have found a strange scoping problem in Sweave.  [...] > >     DM> The strange thing is that while the value in partytotal is >     DM> output correctly as [...] > >     DM> but the dotchart contains the wrong values: it shows sorted >     DM> values, but not sorted names, [...] > No bug, but a feature. :) > (But then, a well known software producer seems to use feature' as a > euphemism for bug', so perhaps I shouldn't call it a feature.) > > There was recently a discussion on r-help (?) about Sweave producing > different output in the text and plot when random numbers were > generated, and it seems as if you have run into the same trap: > >       Code in chunks that produce pictures is executed several times. >       First, to produce the output in the text.  And then once more >       for *each* format in which the figure has to be produced.  I.e., >       if you want a PDF and a PostScript version of the figure, the >       code is executed a total of three times. Thanks, that's what caught me.  Is that a quote from the discussion, or from the docs somewhere?  It makes sense in hindsight, but it's not obvious ahead of time, so it should be stated fairly prominently in the docs. Duncan Murdoch > > All instances of this feature reported so far involved commands that > produced random numbers and the poster was surprised that the output > in the text and the figures differed (and that the two figures were > different). > > In your case the first execution of the code assigns the names to > partytotal, sorts partytotal and produces the data.  On the next > execution, when the picture is produced, partytotal is already sorted > but you reassign the names.  Then the partytotal is sorted again and > the plot is produced.  But that re-assigning of names lead to the > disconnect between values and names. > > Hope this helps. > > Cheers, > >         Berwin > > ========================== Full address ============================ > Berwin A Turlach                      Tel.: +61 (8) 6488 3338 (secr)   > School of Mathematics and Statistics        +61 (8) 6488 3383 (self)       > The University of Western Australia   FAX : +61 (8) 6488 1028 > 35 Stirling Highway                   > Crawley WA 6009                e-mail: [hidden email] > Australia                        http://www.maths.uwa.edu.au/~berwin______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-devel