I would like the end-user functions in my R package (S3 style) to check their arguments and give user informational errors or warnings when a particular check fails.
The obvious (but tedious and incomprehensible) way to do this would be:
foo<-function(aa,bb,cc,dd){ if(length(aa)!=1) stop("The argument 'aa' must have a single value"); if(!is.numeric(aa)) stop("The argument 'aa' must be numeric"); if(!is.character(bb)) stop("The argument 'bb' must be a character"); if(length(bb)>=4||length(bb)<=2) stop("The argument 'bb' must be a vector with a length between 2 and 4"); if(!is.recursive(cc)) stop("The argument 'cc' must be a list-like object"); if(!is.integer(dd)) stop("The argument 'dd' must contain only integers"); if(any(dd<aa)) stop("All values in the argument 'dd' must be greater than the value of argument 'aa'");
I guess I'm not the first to do it. So, can anyone suggest a package that automates all or part of such verification tasks? Or, if itβs not, some short, general idioms that limit ugliness to as few lines as possible in each function?
Thanks.
source share