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

murdoch@stats.uwo.ca murdoch at stats.uwo.ca
Sat Feb 18 20:41:37 CET 2006


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}
<<R>>=
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)
}
@

<<fig=true,width=12,height=6,echo=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



More information about the R-devel mailing list