From ivo.welch at anderson.ucla.edu  Tue Aug 20 20:41:56 2013
From: ivo.welch at anderson.ucla.edu (ivo welch)
Date: Tue, 20 Aug 2013 11:41:56 -0700
Subject: [Rd] Extending suggestion for stopifnot
Message-ID: <CAPr7RtX3b5XBzgn1XT3nPcvX71G=W1CpK=tK1T3zz4zWW=pQNQ@mail.gmail.com>

I am using a variant of stopifnot a lot.  can I suggest that base R
extends its functionality?  I know how to do this for myself.  this is
a suggestion for beginners and students.  I don't think it would break
anything.

first, I think it would be more useful if it had an optional character
string, so users could write

  stopifnot( is.matrix(m), "m is not a matrix" )

this would mean that stopifnot would have to detect whether the last
argument is a string.  (I think stopifnot should have had only one
condition, and one should have used all() to test multiple conditions,
but this is a bridge that was already crossed.)  upon failure,
stopifnot should print the character string.  that's it.


A second enhancement would be a "smart string", which knows that
everything inside {{...}} should be evaluated.

  stopifnot( is.matrix(m), "m is not a matrix, but a {{class(m)}}" )


my own programming variant looks even nicer,

   is.matrix(m) %or% "m is not a matrix but a {{class(m)}}"

but requesting base R to add the %and% and %or% (or, better yet, 'and'
and 'or') operators by default would be pushing my luck.

/iaw


----
Ivo Welch (ivo.welch at gmail.com)



[Rd] Extending suggestion for stopifnot
ivo welch