[R] Sweave question: prevent expansion of unevaluated reused code chunk

Kevin R. Coombes krc at mdacc.tmc.edu
Wed Mar 14 02:18:11 CET 2007


[1] In the example I presented, you're right; I can just use a verbatim 
environment. In the following more complicated example

<<combined,eval=FALSE>
if (some.condition) {
<<chunk1>>
} else {
<<chunk2>>
}
@

I would still want to be able to see the outline of what is going on, 
and a verbatim environment wouldn't work.

[2] You are also correct that there is no advantage if I just call them 
"chunk1" and "chunk2". But if I call them something more interesting, 
like "perform.quantile.normalization" or "truncate.and.log.transform",
then I can use this structure to explain the algorithm at a higher 
level.  If you go back to Knuth's original literate programming 
examples, this is exactly how he presents his examples. For instance, on 
page 104 of the "Literate Programming" book, he has

<Program to print the first thousand prime numbers 2> =
program print_primes(output);
   const m = 1000; <other constants of the program 5>
   var <Variables of the program 4>
     begin <Print the first m prime numbers 3>;
     end.

Of course, he's writing pascal instead of R. But the stuff between <> 
are the program chunks, and he is displaying them without expanding the 
chunks. And, as a result, You get to see a high-level picture of the 
algorithm, keeping the details somewhere else.

Since I'm looking at the results of "weave" in Knuth's example, I don't 
know if he is using a "verbatim" environment or is automatically 
generating this using his implementation of weave for teX and pascal....

But that's the idea.

	Kevin

Seth Falcon wrote:
> "Kevin R. Coombes" <krc at mdacc.tmc.edu> writes:
> 
>> Hi,
>>
>> Consider the following (much simplified) Sweave example:
>>
>> --------------
>>
>> First, we set the value of $x$:
>> <<chunk1,eval=FALSE>>=
>> x <- 1
>> @
>>
>> Then we set the value of $y$:
>> <<chunk2,eval=FALSE>>=
>> y <- 2
>> @
>>
>> Thus, the overall algorithm has this structure:
>> <<combined,eval=FALSE>>=
>> <<chunk1>>
>> <<chunk2>>
>> @
>>
>> <<justDoIt,echo=FALSE>>=
>> <<combined>>
>> @
>>
>> ---------------
>>
>> I'd like to be able to do something like this, where the "combined" 
>> chunk prints out in the final LaTeX document essentially verbatim.  In 
>> particular, I want to see the "<<chunk1>>" unexpanded in that block, 
>> since this gives me a nice conceptual overview of the algorithm. (Of 
>> courser, this is more useful when chunk1 and chunk2 are much longer than 
>> they are in this example....)
>>
>> Is there an option that allows me to get this behavior?
> 
> Maybe I'm not understanding what it is you want, but why not:
> 
> \begin{verbatim}
> <<chunk1>>
> <<chunk2>>
> \end{verbatim}
> 
> What does putting this in an unevaluated chunk buy you?  The
> <<chunkName>> markers are an internal detail of the document and so
> must of the time these never appear in the rendered output.  Even in
> your example, won't it be confusing that <<chunk1>> and <<chunk2>>
> won't have appeared in labels earlier in the rendered document?
> 
> + seth
>



More information about the R-help mailing list