Matthias Burger
2006-Aug-11 20:44 UTC
[R] invisible() - does not return immediately as return() does
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)"
Christos Hatzis
2006-Aug-11 20:59 UTC
[R] invisible() - does not return immediately as return() does
Hi, The difference is in the _return_ value of the function. E.g.> foo <- function() { cat("before\n"); cat("after\n"); return("done")} > foo()before after [1] "done" i.e. returns the return value "done". However> foo2 <- function() { cat("before\n"); cat("after\n"); invisible("done")} > foo2()before after does not show the return value (invisible), but it actually returns it invisibly:> x <- foo2()before after> x[1] "done" HTH. -Christos -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Matthias Burger Sent: Friday, August 11, 2006 4:45 PM To: r-help at stat.math.ethz.ch Subject: [R] invisible() - does not return immediately as return() does 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)" ______________________________________________ R-help at 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.
Seth Falcon
2006-Aug-11 22:11 UTC
[R] invisible() - does not return immediately as return() does
Matthias Burger <matt.burger at 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