Hi, I just started with writing functions in R and so some questions popped up. I provide some values as argument to my function such as: function(a,b,c){} Now i want that the function first checks if the arguments are valid for the function. E.g argument "a" has to be a number in the range 0-1. How can that easily done? So far I have: a <- as.numeric(a) if(0 <= a && a <= 1) to first check if a is a number...if not the function stops and gives an error message. If it is a number it just continues... But how to check the range? Above there is the if-approach but then the rest of the function is exectued as part of if (or else). Is there a simpler way without having the if-brackets around the remaining code? Just a check if the value is between 0 and 1 and if yes continue with the next line if no abort the function with a error message? How can such an error message be created? thank you and best regards, /Johannes --
The quick and dirty way to do so is to use: stopifnot() in conjunction (if necessary with all() and any()). You can replace that first condition with a simple is.numeric() as well. A more helpful way (if this is production code) is to use if statement with the stop() function directly which lets you provide specific error messages. Michael On Tue, Dec 6, 2011 at 6:41 AM, Johannes Radinger <JRadinger at gmx.at> wrote:> Hi, > > I just started with writing functions in R and so some questions popped up. > I provide some values as argument to my function such as: > > function(a,b,c){} > > Now i want that the function first checks if the arguments are valid for the function. E.g argument "a" has to be a number in the range 0-1. How can that easily done? > > So far I have: > > ? ? ? ?a <- as.numeric(a) > ? ? ? ?if(0 <= a && a <= 1) > > to first check if a is a number...if not the function stops and gives an error message. If it is a number it just continues... > > But how to check the range? > Above there is the if-approach but then the rest of the function is exectued as part of if (or else). Is there a simpler way without having the if-brackets around the remaining code? > Just a check if the value is between 0 and 1 and if yes continue with the next line if no abort the function with a error message? How can such an error message be created? > > thank you and best regards, > > /Johannes > -- > > ______________________________________________ > R-help at r-project.org 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.
Adding on Michael comment: If your trap all conditions that render your main code invalid at the beginning of your code, the main code does not have to be within the "else" part of a if else statement. For instance: myf <- function(a,b,c){ if ( ! (is.numeric(a) && (a>=0 & a<=1) ) ){ stop('a is invalid') } if (<all bad conditions for b>){ stop('b is invalid') } if (<all bad conditions for c>){ stop('c is invalid') } return(a+b+c) } Sebastien [[alternative HTML version deleted]]
Hi, thank you... I think I will go for the if-stop approach as the stop() stops the total function... So there is just one little other question: What is the opposite of is.numeric? Is ther isnot.numeric? How can that be implemented in following function: f <- function(a){ if(is.numeric(a)) stop("a is not numeric") if(0 > a && a > 1) stop("a must be a value between 0 and 1") a } /Johannes -------- Original-Nachricht --------> Datum: Tue, 6 Dec 2011 07:04:59 -0500 > Von: "R. Michael Weylandt" <michael.weylandt at gmail.com> > An: Johannes Radinger <JRadinger at gmx.at> > CC: r-help at r-project.org > Betreff: Re: [R] Argument validation within functions> The quick and dirty way to do so is to use: stopifnot() in conjunction > (if necessary with all() and any()). You can replace that first > condition with a simple is.numeric() as well. A more helpful way (if > this is production code) is to use if statement with the stop() > function directly which lets you provide specific error messages. > > Michael > > On Tue, Dec 6, 2011 at 6:41 AM, Johannes Radinger <JRadinger at gmx.at> > wrote: > > Hi, > > > > I just started with writing functions in R and so some questions popped > up. > > I provide some values as argument to my function such as: > > > > function(a,b,c){} > > > > Now i want that the function first checks if the arguments are valid for > the function. E.g argument "a" has to be a number in the range 0-1. How > can that easily done? > > > > So far I have: > > > > ? ? ? ?a <- as.numeric(a) > > ? ? ? ?if(0 <= a && a <= 1) > > > > to first check if a is a number...if not the function stops and gives an > error message. If it is a number it just continues... > > > > But how to check the range? > > Above there is the if-approach but then the rest of the function is > exectued as part of if (or else). Is there a simpler way without having the > if-brackets around the remaining code? > > Just a check if the value is between 0 and 1 and if yes continue with > the next line if no abort the function with a error message? How can such an > error message be created? > > > > thank you and best regards, > > > > /Johannes > > -- > > > > ______________________________________________ > > R-help at r-project.org 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.--
On 06.12.2011 13:46, Johannes Radinger wrote:> Hi, > > thank you... I think I will go for the if-stop approach > as the stop() stops the total function... So there is just > one little other question: What is the opposite of is.numeric? > Is ther isnot.numeric?Yes, see: ?"!" Uwe Ligges> How can that be implemented in following function: > > f<- function(a){ > if(is.numeric(a)) stop("a is not numeric") > if(0> a&& a> 1) stop("a must be a value between 0 and 1") > a > } > > /Johannes > > -------- Original-Nachricht -------- >> Datum: Tue, 6 Dec 2011 07:04:59 -0500 >> Von: "R. Michael Weylandt"<michael.weylandt at gmail.com> >> An: Johannes Radinger<JRadinger at gmx.at> >> CC: r-help at r-project.org >> Betreff: Re: [R] Argument validation within functions > >> The quick and dirty way to do so is to use: stopifnot() in conjunction >> (if necessary with all() and any()). You can replace that first >> condition with a simple is.numeric() as well. A more helpful way (if >> this is production code) is to use if statement with the stop() >> function directly which lets you provide specific error messages. >> >> Michael >> >> On Tue, Dec 6, 2011 at 6:41 AM, Johannes Radinger<JRadinger at gmx.at> >> wrote: >>> Hi, >>> >>> I just started with writing functions in R and so some questions popped >> up. >>> I provide some values as argument to my function such as: >>> >>> function(a,b,c){} >>> >>> Now i want that the function first checks if the arguments are valid for >> the function. E.g argument "a" has to be a number in the range 0-1. How >> can that easily done? >>> >>> So far I have: >>> >>> a<- as.numeric(a) >>> if(0<= a&& a<= 1) >>> >>> to first check if a is a number...if not the function stops and gives an >> error message. If it is a number it just continues... >>> >>> But how to check the range? >>> Above there is the if-approach but then the rest of the function is >> exectued as part of if (or else). Is there a simpler way without having the >> if-brackets around the remaining code? >>> Just a check if the value is between 0 and 1 and if yes continue with >> the next line if no abort the function with a error message? How can such an >> error message be created? >>> >>> thank you and best regards, >>> >>> /Johannes >>> -- >>> >>> ______________________________________________ >>> R-help at r-project.org 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. > > -- > > ______________________________________________ > R-help at r-project.org 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.
Use the ! (not) operator. Not sure what you mean by " as the stop() stops the total function": try the following f <- function(a){ ? ?stopifnot(a > 3) ? ?return(a^2) } f(2) f(4) Michael (PS -- It's usually asked to cc the list so that this all gets threaded properly in folks' mailboxes) On Tue, Dec 6, 2011 at 7:46 AM, Johannes Radinger <JRadinger at gmx.at> wrote:> Hi, > > thank you... I think I will go for the if-stop approach > as the stop() stops the total function... So there is just > one little other question: What is the opposite of is.numeric? > Is ther isnot.numeric? How ?can that be implemented in following > function: > > f <- function(a){ > ? ? ? ?if(is.numeric(a)) stop("a is not numeric") > ? ? ? ?if(0 > a && a > 1) stop("a must be a value between 0 and 1") > ? ? ? ?a > } > > /Johannes > > -------- Original-Nachricht -------- >> Datum: Tue, 6 Dec 2011 07:04:59 -0500 >> Von: "R. Michael Weylandt" <michael.weylandt at gmail.com> >> An: Johannes Radinger <JRadinger at gmx.at> >> CC: r-help at r-project.org >> Betreff: Re: [R] Argument validation within functions > >> The quick and dirty way to do so is to use: stopifnot() in conjunction >> (if necessary with all() and any()). You can replace that first >> condition with a simple is.numeric() as well. A more helpful way (if >> this is production code) is to use if statement with the stop() >> function directly which lets you provide specific error messages. >> >> Michael >> >> On Tue, Dec 6, 2011 at 6:41 AM, Johannes Radinger <JRadinger at gmx.at> >> wrote: >> > Hi, >> > >> > I just started with writing functions in R and so some questions popped >> up. >> > I provide some values as argument to my function such as: >> > >> > function(a,b,c){} >> > >> > Now i want that the function first checks if the arguments are valid for >> the function. E.g argument "a" has to be a number in the range 0-1. How >> can that easily done? >> > >> > So far I have: >> > >> > ? ? ? ?a <- as.numeric(a) >> > ? ? ? ?if(0 <= a && a <= 1) >> > >> > to first check if a is a number...if not the function stops and gives an >> error message. If it is a number it just continues... >> > >> > But how to check the range? >> > Above there is the if-approach but then the rest of the function is >> exectued as part of if (or else). Is there a simpler way without having the >> if-brackets around the remaining code? >> > Just a check if the value is between 0 and 1 and if yes continue with >> the next line if no abort the function with a error message? How can such an >> error message be created? >> > >> > thank you and best regards, >> > >> > /Johannes >> > -- >> > >> > ______________________________________________ >> > R-help at r-project.org 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. > > -- > NEU: FreePhone - 0ct/min Handyspartarif mit Geld-zur?ck-Garantie! > Jetzt informieren: http://www.gmx.net/de/go/freephone