How to conditionally exit from a function?
1
0
Entering edit mode
@shen-weicheng-296
Last seen 10.1 years ago
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
• 678 views
ADD COMMENT
0
Entering edit mode
rgentleman ★ 5.5k
@rgentleman-7725
Last seen 9.4 years ago
United States
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@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@jimmy.harvard.edu | +--------------------------------------------------------------------- ------+
ADD COMMENT

Login before adding your answer.

Traffic: 484 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6