[R] Argument validation within functions

Johannes Radinger JRadinger at gmx.at
Tue Dec 6 12:41:45 CET 2011


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
--



More information about the R-help mailing list