an assert() function
On 12/02/2013 9:11 AM, Greg Minshall wrote:
hi. i've looked for (and not found) an assert() function. needing one, i created the following (from stop()). i'm posting it in case 1) someone sees a problem with this; and 2) someone else has a need.
stopifnot() might do what you want. The tricky thing in writing this sort of function is handling vector inputs. Sometimes x is a vector, and then you need to decide what to do with assert(x > 0) Yours will only test the first component and issue a warning; the base one will test all(x > 0). But the base behaviour causes its own problems; one came up recently in that stopifnot( ncol(x) > 1 ) will not stop if x is just a vector, because ncol(x) is NULL, so ncol(x) > 1 is a length 0 vector, and all(ncol(x) > 1) is TRUE. Duncan Murdoch
cheers, Greg
----
## an assert mechanism...
assert <- function (shouldbe, ...) {
if (!shouldbe) {
.Internal(stop(as.logical(TRUE),
.makeMessage("assertion failure: ", ..., domain = NULL)))
}
}
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.