[R] what does this syntax mean in R

Duncan Murdoch murdoch.duncan at gmail.com
Sun Apr 16 20:14:57 CEST 2017

On 16/04/2017 2:03 PM, Ramnik Bansal wrote:
> ​I am not able to understand the output of the following lines of code.
> *if(TRUE)(print("A"))​*
> Versus
> *if(TRUE){print("A"))*

I assume you have a typo here (or maybe your posting in HTML has done 
more damage than usual.  This line should be


> *In first case I get the ooutput as *
> *>[1]  "A"*
> *>[1]  "A"*

This happens because the print() function both prints the value that was 
passed to it, and also returns it, marked as "invisible". The printing 
gives you the first line.  The parentheses remove the invisibility, so 
the result of the whole expression is "A", *not* marked as invisible, 
and auto-printing gives you the second line.

> *Why does the first case print "A" twice *
> *Why does it not happen with the statement **if(TRUE){print("A"))*

Because braces don't affect invisibility.  Auto-printing ignores objects 
that are marked as invisible.  You would get two lines printed if you 
asked for explicit printing, e.g.

print( if(TRUE){print("A")} )

or remove the invisibility later, e.g.

( if(TRUE){print("A")} )

Duncan Murdoch

> *Thanks *
> *Ramnik*
> 	[[alternative HTML version deleted]]
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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