Kevin R. Coombes
2007-Mar-13 23:02 UTC
[R] Sweave question: prevent expansion of unevaluated reused code chunk
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? Thanks, Kevin
Seth Falcon
2007-Mar-13 23:56 UTC
[R] Sweave question: prevent expansion of unevaluated reused code chunk
"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 -- Seth Falcon | Computational Biology | Fred Hutchinson Cancer Research Center http://bioconductor.org
Duncan Murdoch
2007-Mar-14 10:53 UTC
[R] Sweave question: prevent expansion of unevaluated reused code chunk
On 3/13/2007 7:02 PM, Kevin R. Coombes wrote:> 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?As others have said, the answer is currently no, but in R 2.5.0 this should be a relatively easy modification (because it has the ability to echo your input, rather than a deparsed version of it). In the other platforms you've used, is there a standard syntax to indicate whether or not you want the chunks expanded? I can see either behaviour as being desirable in different circumstances. Sometimes you want the reader to know about your chunk names, and sometimes you don't. Duncan Murdoch