Mention the case of logical(0) in ?stopifnot

Previous Topic Next Topic
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Mention the case of logical(0) in ?stopifnot

Scott Kostyshak-2
I wonder if it would be helpful to mention in ?stopifnot that
stopifnot(logical(0)) does not give an error (for background on why this
is the case, see [1]). For example, ?all explicitly mentions the

  That all(logical(0)) is true is a useful convention

and includes an example:

  all(logical(0))  # true, as all zero of the elements are true.

I think it would be nice to give examples in ?stopifnot of calls that
are not ideal uses of the function, such as the poorly written
stopifnot() call that I recently wrote:

  x <- 1:5
  # does not give an error
  stopifnot(ncol(x) == 2)
  # gives an error
  stopifnot(identical(ncol(x), 2L))

Or this code from [2]:

  li <- list()
  li$item <- 1
  # Does not give an error, because
  # "item" is misspelled and "NULL == 0" returns logical(0)
  stopifnot(li$tem == 0)

I think that a useful way to teach users how to use a function is to
teach them how not to use it.

Would a patch for the documentation along these lines be considered?

By the way, there are some regression tests in base R that rely on the
behavior of stopifnot(logical(0)), where the logical(0) results from
`==`. I can make a list of these tests if someone thinks it would be a
good idea to double-check them and possibly improve them (e.g., convert
them to use identical() instead of `==`). I'm guessing it's not worth
the time.



Scott Kostyshak
Assistant Professor of Economics
University of Florida

[hidden email] mailing list