[Rd] Extending suggestion for stopifnot

William Dunlap wdunlap at tibco.com
Tue Aug 20 23:00:49 CEST 2013


> my own programming variant looks even nicer,
>    is.matrix(m) %or% "m is not a matrix but a {{class(m)}}"

But it does not act nicely.  All %anything% constructs have the
same precedence, that of %*%.   Hence
   x==3 %or% "x is not three"
is parsed as
  x == ( 3 %or% "x is not three" )
which is not what I think you want.

Use standard functional notation, e.g., ensureThat(condition, message), and
you don't get into parsing trouble.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: r-devel-bounces at r-project.org [mailto:r-devel-bounces at r-project.org] On Behalf
> Of ivo welch
> Sent: Tuesday, August 20, 2013 11:42 AM
> To: r-devel at r-project.org List
> Subject: [Rd] Extending suggestion for stopifnot
> 
> 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)
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



More information about the R-devel mailing list