[R] (S|odf)weave : how to intersperse (\LaTeX{}|odf) comments in source code ? Delayed R evaluation ?

Duncan Murdoch murdoch.duncan at gmail.com
Sun Dec 12 14:05:04 CET 2010


On 11/12/2010 5:58 PM, Emmanuel Charpentier wrote:
> Dear list,
>
> Inspired by the original Knuth tools, and for paedaogical reasons, I wish
> to produce a document presenting some source code with interspersed
> comments in the source (see Knuth's books rendering TeX and metafont
> sources to see what I mean).
>
> I seemed to remember that a code chunk could be defined piecewise, like in
>
> Comments...
>
> <<Chunk1, eval=FALSE, echo=TRUE>>=
> SomeCode
> @
>
> Some other comments...
>
> <<Chunk2, eval=FALSE, echo=TRUE>>=
> MoreCode
> @
>
> And finally,
>
> <<Chunk3, eval=TRUE, echo=TRUE>>=
> <<Chunk1>>
> <<Chunk2>>
> EndOfTheCode
> @
>
> That works ... as long as SomeCode, MoreCode and EndOfTheCode are self-
> standing pieces of R code, but *not* code fragments. You can *not*
> intersperse comments in, say, a function body, or  local() environment
> this way : when Sweaving, *R* complains of an incomplete source (makes
> noise about an unexpected end of input at the end of Chunk1, IIRC, and
> never sees Chunk2).
>
> I hoped that Sweave's "alternative" syntax could offer a way out : no
> such luck.
>
> There seems to be no way to delay R evaluation of a R chunk passed by
> Sweave ; at least, the "eval=FALSE" option of chunk declaration is not
> sufficient for that.


It's not evaluation, it's parsing, but you're right:  there's no way to 
delay it.  Perhaps Sweave needs a "parse=FALSE" option.


>
> Am I missing something in the Sweave nd odfWeve documentations (that I
> read till I grew green and moldy) ? Or does this require a fundamental
> change in the relevant Sweave drivers ?

I suspect the latter.

>
> Can you suggest alternative ways of doing what I mean to do ? The only
> workaround I found is to paste a second copy of my code in a \verbatim
> environment (or, in the case of odfWeave, in the "text" part), and spice
> it with \end{verbatim} comments.. \begin{verbatim} chunks. This way, I
> lose any guarantee of consistency between commented text and effective
> code.

Not any very satisfactory suggestions:

- write smaller functions, and make up your large function out of calls 
to them.  Then you can define a whole small function in one chunk.

- use keep.source=TRUE, and use R comments rather than nicely typeset 
ones if you need to make comments within a function definition.

I don't know any way to get what you were asking for, but perhaps these 
are close enough.

Duncan Murdoch

>
> Any other idea ?
>
> 					Emmanuel Charpentier
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list