[Rd] Extending suggestion for stopifnot

Deepayan Sarkar deepayan.sarkar at gmail.com
Tue Aug 20 21:30:38 CEST 2013


On Wed, Aug 21, 2013 at 12:11 AM, ivo welch <ivo.welch at anderson.ucla.edu> wrote:

> 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" )

How is this better/nicer/more preferable than, say,

if (!is.matrix(m)) stop("m is not a matrix")

?

I think stopifnot() is mostly meant for regression tests and sanity
checks, and should not be used instead of stop() if you want nicely
formatted error messages.

-Deepayan


> 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)
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



More information about the R-devel mailing list