Re: [R] invisible() - does not return immediately as return() does

From: Seth Falcon <sfalcon_at_fhcrc.org>
Date: Sat 12 Aug 2006 - 08:11:02 EST

Matthias Burger <matt.burger@web.de> writes:

> Hi,
>
> I stumbled across the following (unexpected for me) behavior after
> replacing a return() statement in the middle of a function by invisible().
>
> Example:
> foo <- function() { cat("before\n"); return(); cat("after\n")}
>>foo()
> before
> NULL
>
> foo2 <- function() { cat("before\n"); invisible(TRUE); cat("after\n")}
>>foo2()
> before
> after
>
> I expected invisible to have the same behavior as return, namely
> immediately return execution to the calling environment.
>
> I rechecked ?invisible and ?return
> and here I read in section 'See Also'
> [...]
> 'invisible' for 'return(.)'ing _invisibly_.
>
> Do I just misunderstand what this implies?
> Put another way what is the intention behind invisible() continuing
> until the last statement before returning? ?invisible does not hint at
> this.

I can understand the confusion, but I think invisible is intended to return its argument "invisibly" and actually has nothing to do with returning from a function (except that is where you are going to use it almost always ;-).

So you want return(invisible(foo)) in the middle of a function.

The man page for invisible says:   

   Return a (temporarily) invisible copy of an object.

But the man page for return has the return(.)'ing _invisibly_ statement which I think is confusing.

Cheers,

+ seth



R-help@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. Received on Mon Aug 14 02:00:42 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Mon 14 Aug 2006 - 04:19:07 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.