[R-gregmisc-users] SF.net SVN: r-gregmisc: [1115] trunk/SASxport
Brought to you by:
warnes
From: <wa...@us...> - 2007-08-03 01:44:25
|
Revision: 1115 http://r-gregmisc.svn.sourceforge.net/r-gregmisc/?rev=1115&view=rev Author: warnes Date: 2007-08-02 18:44:23 -0700 (Thu, 02 Aug 2007) Log Message: ----------- Add "assert" function to avoid dependenct on gtools. Added Paths: ----------- trunk/SASxport/R/assert.R trunk/SASxport/man/assert.Rd Added: trunk/SASxport/R/assert.R =================================================================== --- trunk/SASxport/R/assert.R (rev 0) +++ trunk/SASxport/R/assert.R 2007-08-03 01:44:23 UTC (rev 1115) @@ -0,0 +1,6 @@ +## useful function, raises an error if the FLAG expression is FALSE +assert <- function( FLAG ) + { + if(!all(FLAG)) + stop("Failed Assertion") + } Added: trunk/SASxport/man/assert.Rd =================================================================== --- trunk/SASxport/man/assert.Rd (rev 0) +++ trunk/SASxport/man/assert.Rd 2007-08-03 01:44:23 UTC (rev 1115) @@ -0,0 +1,42 @@ +\name{assert} +\alias{assert} +\title{Generate an error if an expression is not true.} +\description{ + Generate an error if an expression is not true. +} +\usage{ +assert(FLAG) +} +\arguments{ + \item{FLAG}{ Expression that should evaluate to a boolean vector} +} +\details{ + Assert generate an error if its aregument does not evaluate to + boolean (vector) containing only \code{TRUE} values. This is useful + for defensinve programming as it provides a mechanism for checking + that certain facts, the 'assertions', do in fact hold. Checking of + 'assertions' is an important tool in the development of robust program + code. +} +\value{ + None. Evaluated only for its side effect. +} +\author{Gregory R. Warnes \email{wa...@bs...} } +\seealso{ \code{\link[base]{stop}}, \code{\link[base]{warning}} } +\examples{ + +## Trivial example +posSqrt <- function(x) + { + assert(x>=0) + sqrt(x) + } + +posSqrt(1:10) # works fine, no messages +\dontrun{ +posSqrt(-5:5) # generates an error, since the asssertion is not met +} + + +} +\keyword{programming} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |