what does this syntax mean in R

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

what does this syntax mean in R

ramnikbansal
​I am not able to understand the output of the following lines of code.

*if(TRUE)(print("A"))​*

Versus

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

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

*Why does the first case print "A" twice *

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

*Thanks *
*Ramnik*

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: what does this syntax mean in R

Duncan Murdoch-2
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

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


>
> *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]]
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Loading...