[BioC] How to conditionally exit from a function?
Robert Gentleman
rgentlem at jimmy.harvard.edu
Wed May 21 19:00:31 MEST 2003
You exit from a function in one of two ways,
call return, or fall off of the end (you can of course call return as
the last line in the function).
Functions *always* return a value (it is not always printed). It is
the value of the last statement evaluated.
So for the function below, if x<0 it returns the string "ouch",
otherwise it returns x+10
foo <- function(x) {
if(x<0) return("ouch")
y<-10
x+y
}
On Wed, May 21, 2003 at 05:48:01PM -0400, SHEN, WEICHENG . wrote:
>
> Hi all:
>
> I need to conditionally exit from a function, but don't know how. My example
> below indicates that when x>0, I wish to immediately exit from the function
> foo. I also with to return a flag value so that I could find out what
> happend in executing this function. What command I can use to make it
> happen? Your help and suggestions will be greatly appreciated. Thanks!
>
> ============================================================================
> ===================================
>
> foo<-function (x)
> {
> if (x>0) {
> do something ....
> } else {
> # need to exit from this function at this point, and also
> return a flag value
> }
> do something ...
> }
>
> Weicheng Shen, Ph.D.
> Senior Image Analyst
> Science Applications International Corporation
> 1710 SAIC Drive
> Mail Stop 2-6-9
> McLean, VA 22102
> (703) 676-4189
>
> _______________________________________________
> Bioconductor mailing list
> Bioconductor at stat.math.ethz.ch
> https://www.stat.math.ethz.ch/mailman/listinfo/bioconductor
--
+---------------------------------------------------------------------------+
| Robert Gentleman phone : (617) 632-5250 |
| Associate Professor fax: (617) 632-2444 |
| Department of Biostatistics office: M1B20 |
| Harvard School of Public Health email: rgentlem at jimmy.harvard.edu |
+---------------------------------------------------------------------------+
More information about the Bioconductor
mailing list