[R] type checking --- just a thought

Wiener, Matthew matthew_wiener at merck.com
Fri May 14 16:43:46 CEST 2004


Ivo --

The "stopifnot" statement can be used to do this sort of check.  For
example, if you need to check that one of your arguments is a vector with
length >= N, you can add

stopifnot(is.vector(myarg))
stopifnot(length(myarg) >= N)

To the beginning of your function.  This will throw an error if one of the
conditions is not met.  The rest of your conditions can be checked
similarly.

Hope this helps,

Matt Wiener



-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of ivo welch
Sent: Friday, May 14, 2004 10:23 AM
To: r-help at stat.math.ethz.ch
Subject: [R] type checking --- just a thought



hi:  would it be useful to build into R an optional mechanism that 
typechecks arguments?  for example,

    sum.across <- function (  inpmatrix : matrixtype( dim[1]>1, dim[2]>3 
) ) : vector { }
       # this would define a sum.across function that can take matrices 
or data sets, but not vectors,
       # and which indicates that it will return a vector.

    xsum <- sum.across( 1:10 );  # error

    repeat <- function( series : vector( dim>0 ),  times : scalar( 
value>0 ) ) : vector;

similarly, a common input error condition may be calling a function with 
a NULL vector, or with a vector with fewer than N observations.  many 
statistical functions have such hard-wired limits.  I know that "if" 
statements can do this, but this might make for a nice standardized 
language feature.  on the other hand, the effort and complexity may not 
be worth the extra functionality.

and one beg to the language maintainers for something that I hope is simple:

    in R 1.8.2, please add to the "source" function information where 
(file:linenumber) dies or ends.

regards,

/ivo

______________________________________________
R-help at stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html




More information about the R-help mailing list