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
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
S4 methods take care of some of this checking (more specification needed). best, -tony ivo welch <ivo.welch at yale.edu> writes:> 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 >-- rossini at u.washington.edu http://www.analytics.washington.edu/ Biomedical and Health Informatics University of Washington Biostatistics, SCHARP/HVTN Fred Hutchinson Cancer Research Center UW (Tu/Th/F): 206-616-7630 FAX=206-543-3461 | Voicemail is unreliable FHCRC (M/W): 206-667-7025 FAX=206-667-4812 | use Email CONFIDENTIALITY NOTICE: This e-mail message and any attachme...{{dropped}}
ivo welch <ivo.welch at yale.edu> writes:> 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.It's not simple because it is not a simple line-by-line evaluation. The whole file is parsed and the resulting code then executed, by which time the line information has been lost. If you can come up with a well thought out scheme for retaining it, patches might be considered. (There's not much chance of getting an R 1.8.2, though.) -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907