[R] stopifnot() doesnt work as I expect it to. Are my expectations correct?

Thierry Onkelinx thierry.onkelinx at inbo.be
Fri May 20 17:19:05 CEST 2016


Dear Bill,

assertthat has such capabilities

f <- function(df){
  require(assertthat)
  assert_that(is.data.frame(df))
  assert_that(is.integer(df$ID))
  range(df$ID)
}

f(data.frame(ID=4:7))
# [1] 4 7
f(4:7)
# Error: df is not a data frame
f(data.frame(ID=letters))
#  Error: df$ID is not an integer vector

Best regards,


ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and
Forest
team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
Kliniekstraat 25
1070 Anderlecht
Belgium

To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to say
what the experiment died of. ~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data. ~ Roger Brinner
The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey

2016-05-20 16:33 GMT+02:00 William Dunlap via R-help <r-help op r-project.org>:

> The following usage of stopifnot seems reasonable to me and it
> would be nice if the 2nd call caused the message 'is.data.frame(df) is not
> TRUE'.
>
> f <- function(df) {
>     stopifnot(is.data.frame(df), is.integer(df$ID))
>     range(df$ID)
> }
> f(data.frame(ID=4:7))
> # [1] 4 7
> f(4:7)
> # Error in df$ID : $ operator is invalid for atomic vectors
>
>
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com
>
> On Fri, May 20, 2016 at 7:13 AM, Duncan Murdoch <murdoch.duncan op gmail.com>
> wrote:
>
> > On 20/05/2016 4:44 AM, Vasanth Mohan wrote:
> >
> >> Hi,
> >>
> >> *stopifnot(FALSE, someOtherExpression)*
> >>
> >> For the above code I expect stopifnot() to always say that 'FALSE is not
> >> TRUE' regardless of what someOtherExpression is(It may evaluate to TRUE
> or
> >> FALSE or throw an error). Is my expectation correct? Is that how
> >> stopifnot() is supposed to work?
> >>
> >> The present implementation of stopifnot() does not work like that. If
> >> someOtherExpression would throw an error, then stopifnot() throws that
> >> error instead of saying 'FALSE is not TRUE'.
> >>
> >> So, I modified the source code of stopifnot() and now it works as I
> expect
> >> it to.
> >> If that is how stopifnot() is supposed to work, then kindly let me know
> >> how
> >> I can contribute my solution
> >>
> >>
> > The documentation is unclear on that.  First it implies all expressions
> > are evaluated:  "If any of the expressions in ... are not all TRUE, stop
> is
> > called, producing an error message indicating the first of the elements
> of
> > ... which were not true."
> >
> > But then the "conceptually equivalent" code acts the way you expected.
> >
> > However, it doesn't really make sense to me to put in tests that could
> > themselves trigger errors unless you'd be interested in seeing those
> > errors, so I don't think I'd change it.
> >
> > Duncan Murdoch
> >
> > ______________________________________________
> > R-help op r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > 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.
> >
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help op r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list