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

Matthias Burger matt.burger at web.de
Fri Aug 11 22:44:32 CEST 2006


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.


Regards,

  Matthias


>R.version.string
[1] "Version 2.3.1 (2006-06-01)"

same behavior in R 2.2.1 or
R.version.string
[1] "R version 2.4.0 Under development (unstable) (2006-07-29 r38715)"



More information about the R-help mailing list