[R] Vector of Numbers Not Output to Screen

Duncan Murdoch murdoch.duncan at gmail.com
Mon Jul 21 02:49:52 CEST 2014

On 20/07/2014, 5:46 PM, David Winsemius wrote:
> On Jul 20, 2014, at 6:30 AM, Duncan Murdoch wrote:
>> On 17/07/2014, 10:00 PM, Dario Strbenac wrote:
>>> The example in the question was not inside a user function.
>> The explanations you were given were slightly inaccurate.  The usual
>> rule is that results returned at the top level are printed unless they
>> are marked as invisible.  (There are a few cases where "top level" is
>> faked, e.g. in example code, and in Sweave.)
>> When you do something like
>> if(test) { a; b; c }
>> you have an expression that returns NULL invisibly if the test is FALSE,
>> and returns the value of the block (i.e. c) visibly if it is TRUE.  It
>> is the value of the if that is printed.
>> There is no difference in the handling of a, b and c:  each is an
>> expression that returns the value of the corresponding variable without
>> marking it as invisible.  But none of them are top-level expressions, so
>> none of them print.
> I'm not sure what that last one was intended to mean but it seemed to imply that nothing would be printed if those expressions had values (even if the interpreter were able to find values in one of hte enclosing environments). That would not be what I expected. I think of curved-braces as a function and the results of the last evaluation would be expected to be returned:

I don't understand your misunderstanding.  The expression "{ a; b; c }"
(ignore the quotes here and later) is found by evaluating a, then
evaluating b, then evaluating c, and the value of c is returned as the
value of the whole expression.  Braces don't affect visibility, so if c
is 4 and no error occurs, the value of "{ a; b; c }" is a visible 4.

>> test <- TRUE
>> a=2;b=3;c=4
>> if(test){a;b;c}
> [1] 4

In this case, the expression isn't "{ a; b; c }", it's "if (test) { a;
b; c }".  Since test is TRUE, that returns the value of "{ a; b; c }"
visibly, i.e. the value is 4.

If test had been FALSE, the value would be NULL, marked as invisible.
Braces don't affect visibility, but parens do, so

if (FALSE) 4


{if (FALSE) 4}

both print nothing, but

(if (FALSE) 4)

will print NULL.  All three versions have the value NULL, as you could
see if you assigned them to a variable, e.g.

x <- if (FALSE) 4
x <- {if (FALSE) 4}
x <- (if (FALSE) 4)

which will print NULL three times.

> If one of them had no value, an error would be thrown.
>> rm(b)
>> if(test){a;b;c}
> Error: object 'b' not found

I don't see what this has to do with the previous discussion.  If you
thought I was talking about errors in expressions, you misunderstood me.

Duncan Murdoch

More information about the R-help mailing list