[R] comment lines sometimes removed from a function on exit from internal R editor

Duncan Murdoch murdoch.duncan at gmail.com
Tue Feb 14 13:11:41 CET 2012


On 12-02-13 6:02 PM, Cleridy Lennert wrote:
> Dear All -
> The problem: comment lines in an R function (lines beginning with # ) are *sometimes* removed on leaving the R default editor (same with notepad).
> I'm working on a Windows machine with R version 2.14.1.
> An example is below. Couldn't find anything that seemed to relate to this in the Changelog. I don't recall encountering this behavior with previous versions of R. Any suggestions on how to preserve the comment lines would be greatly appreciated. Many apologies if this is a stupid question. Cleridy

You are likely seeing the results of dropping the source attribute that 
happened in 2.14.0.

> ## this function has comments at the top:
>
>> mapsp.sqrtcpd.f
>
> function (input.frm, cpd.numerator, cpd.denominator, basis.dim,
>
>        npts, nyrs.unq, prop.ctch)
>
> {
>
> # here are some comments
>
> # and more
>
> #
>
>        year.min<- 1975
>
> ......
>
>
>
> ## copy function to a dummy function:
>
>> tmp.f<-mapsp.sqrtcpd.f
>
>
>
> ## now edit it with fix and remove a ) so that it complains:
>
>>   fix(tmp.f)
>
> Error in edit(name, file, title, editor) :
>
>      unexpected symbol occurred on line 15 use a command like
>
> x<- edit()
>
> to recover
>
>
>
> ## and now recover it and add back in the ) that had been removed :
>
>> tmp.f<-edit()
>
>
>
> ## and look to see what happened... no comments at the top anymore..
>
>> tmp.f
>
> function (input.frm, cpd.numerator, cpd.denominator, basis.dim,
>
>        npts, nyrs.unq, prop.ctch)
>
> {
>
>        year.min<- 1975
>
> ......
>

I'll take a look at edit() and see if it is unnecessarily dropping the 
source references which replaced the source attribute.  But another way 
to maintain your code where this would not be a problem is to keep it in 
files outside of R, and source() them after changes.  That does work to 
keep the comments.

Duncan Murdoch



More information about the R-help mailing list