Re: [Rd] literate programming

From: Kevin R. Coombes <>
Date: Tue, 05 Aug 2008 10:45:59 -0500

Hi Terry,

You can do this with Sweave (and something smart like emacs with ESS installed as your editor), but you have to work at it a little. The key is the fact that a couple of releases ago they added options for "keep.source" and "expand". For example, you do the following:

First, describe the various steps in the algorithm. (Unless you are defining functions to use later, you probably do not want to evaluate these.)

# code here to get the parameters

# code here to split data into training and test sets @

# code here to fit a model to training data @

# code here to see how well the model works @

Then you can put the pieces together, doing something like

<<runSplits,keep.source=TRUE,expand=FALSE>>= for (i in 1:numberOfSplits) {



The "expand=FALSE" makes sure that the final report does not re-expand the lines of code in the displayed output, which allows you to focus on the structure of the algorithm.

There are still two weaknesses compared to Knuth's original idea: [1] You cannot describe the overall algorithm first but wait until later to define the pieces. (Actually, I could be wrong about this; it just occurred to me that you might be able to manage this with yet another clever use of "eval=FALSE", but I haven't tried that.)

[2] The names that you assign to the code chunks do not appear in the report automatically, so you have to write text in front of them to make them show up. Without these, the references in the final piece do not necessarily make sense to the reader trying to follow the action.



Terry Therneau wrote:
> I'm working on the next iteration of coxme. (Rather slowly during the summer).
> This is the most subtle code I've done in S, both mathematically and
> technically, and seems a perfect vehicle for the "literate programming" paradym
> of Knuth. The Sweave project is pointed at S output however, not source code.
> I would appreciate any pointers to an noweb type client that was R-aware.
> Other suggestions are welcome as well. At the end of the day I'd like to have
> a good user guide, technical reference, and solid enough code documentation that
> others can begin to participate as well. (Retirement in 10 years -- I don't
> expect to maintain this forever!)
> Terry Therneau
> ______________________________________________
> mailing list
> mailing list Received on Tue 05 Aug 2008 - 15:50:14 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 05 Aug 2008 - 18:36:05 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive