[Rd] Source references from the parser

Duncan Murdoch murdoch at stats.uwo.ca
Sun Nov 26 12:30:06 CET 2006

On 11/26/2006 3:02 AM, Deepayan Sarkar wrote:
> On 11/25/06, Duncan Murdoch <murdoch at stats.uwo.ca> wrote:
>> On 11/25/2006 11:00 PM, Deepayan Sarkar wrote:
>>> On 11/25/06, Duncan Murdoch <murdoch at stats.uwo.ca> wrote:
>>>> On 11/25/2006 3:12 PM, Deepayan Sarkar wrote:
>>>>> On 11/25/06, Duncan Murdoch <murdoch at stats.uwo.ca> wrote:
>>>>>> I have just committed some changes to R-devel (which will become R 2.5.0
>>>>>> next spring) to add source references to parsed R code.  Here's a
>>>>>> description of the scheme:
> [snipped]
>>>>>> Comments and problem reports are welcome.
>>>>> I haven't tested this, but the idea seems useful. Will this have any
>>>>> effect on code parsed using parse(text = "...")? Can it be extended to
>>>>> have some such effect? I ask because this is relevant in the context
>>>>> of Sweave, where I have always wanted the ability to retain the
>>>>> original formatting. I'm currently testing a patch that allows me to
>>>>> do this specifically for Sweave, but a more general solution is
>>>>> obviously preferable.
>>>> I've just added the capability to Sweave.  I haven't committed yet,
>>>> because I think it's important that authors can choose whether or not to
>>>> turn this on.  Could you let me know your typical workflow with Sweave,
>>>> whether you'd like this to default to on or off, and where you'd expect
>>>> to change the default?
>>> I would like it as an option to the RweaveLatex driver (and perhaps
>>> others). In terms of changing the API, this is as simple as adding an
>>> argument to the 'RweaveLatexSetup' function.
>>> In the case of my patch, the default is off, and is turned on by
>>> <<...,src=TRUE>>
>>> ...
>>> @
>>> To make this the global default, one can do
>>> \SweaveOpts{src=TRUE}
>>> etc. (the name 'src' is not necessarily the best, some variant of
>>> 'keep.source' might be more intuitive.)
>> This is now committed.
> Working great, thanks. The behaviour of comments is interesting (and
> fortuitous for me), in that top level comments get associated with the
> expression that follows it and comments at the end of a chunk are
> ignored. Is this intended and can I expect it to remain unchanged? (My
> chunks currently tend to end with a blank comment to work around a bug
> in ESS, and I would rather not fix them all.)

I'd say it's intentional but subject to change in the next few weeks, 
depending on the reaction to it.  It behaves the same when running 
examples in man pages.  As far as I know it suits the comment style used 
in the base packages well.  Since comments haven't been displayed in the 
past, I can't see how it would inconvenience anyone too much, but you 
never know...

browser() behaves differently during debugging:  there neither preceding 
nor following comments are shown.  My hope is that a source browser will 
be made easier by these source references, so I don't intend to change 

Duncan Murdoch

>> I used keep.source, exactly the same as the option() that controls this
>> behaviour in other places.
>> I decided to set the default to TRUE.  This means vignettes will all
>> look different in R-devel.  The simplest way to get the previous
>> appearance is to put in
>> \SweaveOpts{keep.source=FALSE}
>> but in most cases I think people will want the new behaviour.  It's only
>> bad if the code was badly formatted or contained comments you don't want
>> to show up in the final document.  I looked through the grid package
>> vignettes, and only saw about half a dozen places where I thought the
>> formatting needed tweaking.
> Fine with me (although I suspect R's deparse will often produce more
> readable code if the original wasn't written in an editor with smart
> indentation).

More information about the R-devel mailing list