[R] [Fwd: Re: Block comments in R?]

Philippe Grosjean Philippe.Grosjean at umh.ac.be
Thu Oct 5 14:27:55 CEST 2006


Ooops! Sorry, I send it only to Uwe Ligges the first time.
Best,

Philippe Grosjean

This is perhaps another solution, more elegant in the way the block
comment is written... but it requires to redefine `!` and slows it a
little bit because it tests first its arguments before calling
.Primitive(!):

It takes advantage of `!` being not defined for character arguments:

> !2
[1] FALSE
> !"some text"
Error in !"some text" : invalid argument type

So, now, we will define it for character arguments (invisibly returns
the argument)

> `!`<- function(x)
+	if (inherits(x, "character") == FALSE)
+		.Primitive("!")(x) else invisible(x)


Now, `!` can be used to construct a block comment:

==== A R script with block comments =====

1+1	# This is a line comment

!'
This is a block comment spread
on several lines...
'

ls()

!!'
This is another block comment, possibly of higher
importance than the previous one
'
search()

!!!'
For color syntax highlighting and to better detect
the end of block comments, one may also decide to
use the same code for opening and closing the comments
like it is the present case
!!!'

!'
Note that the only constraint is to escape single quotes
in block comments (or not to use single quotes)
Of course, one could also decide to use double quotes
instead of single quotes
!'

!'
Now, it would be nice to have a little patch of .Primitive(!)
that simply displays no error message in case the argument of
`!`is a character sting. So, the hock would not be required
any more
!'

==== And of the R script =====

Best,

Philippe Grosjean


Uwe Ligges wrote:
> 
> Robin Hankin wrote:
>> On 5 Oct 2006, at 10:05, Uwe Ligges wrote:
>>
>>>
>>> Wee-Jin Goh wrote:
>>>> Hello list,
>>>>
>>>> Is there any way to perform a block comment in R? In C++, anything in
>>>> between a /* and */ is considered a comment, and it allows
>>>> programmers to comment out chunks of code for testing and debugging.
>>>> Is there such a feature in R?
>>>
>>> This has frequently been asked on the list. Try to google for it. You
>>> will find answers like
>>>
>>> if(FALSE){"
>>>      code block
>>>      commented
>>> "}
>>>
>>>
>>
>> That method doesn't work for me:
>>
>> if(FALSE){"
>>
>> if(1<2)
>>   print("1<2")
>> else
>>   print("1>2")
>>
>> "}
> 
> 
> Use an editor that comments out a whole block which is what I do all the 
> time, e.g. use Tinn-R, Emacs or WinEdt, to mention just a few of them.
> 
> Or you can go ahead and use
> 
> if(FALSE){
> 
> if(1<2)
>    print("1<2")
> else
>    print("1>2")
> 
> }
> 
> 
> or
> 
> if(FALSE){'
> 
> if(1<2)
>    print("1<2")
> else
>    print("1>2")
> 
> '}
> 
> 
> Uwe
> 
> 
>> returns an error.  How would I comment out that block of (incorrect) code?
>>
>>
>>
>>
>>
>>
>>
>>> or "use a good editor that supports commenting and uncommenting blocks".
>>>
>>>
>>> Uwe Ligges
>>>
>>>
>> -- 
>> Robin Hankin
>> Uncertainty Analyst
>> National Oceanography Centre, Southampton
>> European Way, Southampton SO14 3ZH, UK
>>  tel  023-8059-7743
>>
>>
>>
>>
> 
> ______________________________________________
> R-help at stat.math.ethz.ch 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