[R] Sweave problem with recursive call

g.wrobel@dkfz-heidelberg.de
Fri, 12 Jul 2002 09:33:53 -0000

Hi,

while trying to play around with Sweave I found out that I was unable to call
Sweave in a code chunk that is being parsed by Sweave.

Code chunk that is being parsed:

<<>>=
for (i in 1:length(chips[,1])) {
  createAnalysis(as.character(chips[i,1]),i)
}
system(paste("./concatenateAnalysis -",i,sep=""))
@

The function createAnalysis that is called:

createAnalysis <- function(curChip, counter) {
  write(curChip,"direct.chip")
  Sweave("analyseChip.Rnw")
  system(paste("mv analyseChip.tex
aC_",counter,".tex",sep=""))
}

And the code chunk from analyseChip.Rnw:

<<>>=

gpr <- gpr.load.file(curChip,gpr.load.std)

@

\begin{description}
  \item[Filename] \Sexpr{curChip}
  \item[Date] \Sexpr{gpr$DateTime}
\end{description}

That leads to the follwing error:

>  require(tools)
Loading required package: tools
[1] TRUE
> Sweave("analyse.Rnw")
Writing to file analyse.tex
Processing code chunks ...
 1 : echo term verbatim
 2 : echo term verbatim
 3 : echo term verbatim
Read 1 items
Read 6144 records
Read 1 records
Error: can not set length of non-vector
Error in driver$runcode(drobj, chunk, chunkopts) :
        Error: can not set length of non-vector

Basically Sweave creates a text connection to an object called
"output" in the
main environment while evaluating a code chunk. When calling Sweave in a code
chunk this "output" is referenced with several textconnections. I
attached a
diff file with a suggestion how to circumvent the problem. It can be applied
to the Sweave.R file in the tools-1.5.0.tar.gz archive.

Greetings

Gunnar Wrobel

-------------- next part --------------
286c286,292
<         tmpcon <- textConnection("output", "w")
---
>         if
(is.finite(max(as.integer(sub("output","",apropos("output"))),na.rm=TRUE)))
{
>           oc <-
max(as.integer(sub("output","",apropos("output"))),na.rm=TRUE)+1
>         } else {
>           oc <- 0
>         }
>         tmpcon <-
textConnection(paste("output",oc,sep=""), "w")
>         output <- get(paste("output",oc,sep=""))