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
following:

  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


[1]
https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_pipermail_r-2Dhelp_2015-2DDecember_434610.html&d=DwIBAg&c=pZJPUDQ3SB9JplYbifm4nt2lEVG5pWx2KikqINpWlZM&r=1fpq0SJ48L-zRWX2t0llEVIDZAHfU8S-4oINHlOA0rk&m=G8tEZpMWPL4vxGGinNsRHdfXpDqiFEownNAdY_AOiUk&s=wxOygcK0MIUDAQhkzjgfT-4edxWNCWluOEgAyR-xCC0&e=
[2]
https://urldefense.proofpoint.com/v2/url?u=https-3A__stackoverflow.com_questions_33670060_how-2Dto-2Dhave-2Dstopifnot-2Dreturn-2Dan-2Derror-2Dwhen-2Dcalled-2Don-2Da-2Dmissing-2Dnull-2Delement&d=DwIBAg&c=pZJPUDQ3SB9JplYbifm4nt2lEVG5pWx2KikqINpWlZM&r=1fpq0SJ48L-zRWX2t0llEVIDZAHfU8S-4oINHlOA0rk&m=G8tEZpMWPL4vxGGinNsRHdfXpDqiFEownNAdY_AOiUk&s=ZCSz07Z1Gz4pAWgw75UUn9wIMI-wCv2Srfkn2MGYYlI&e=


--
Scott Kostyshak
Assistant Professor of Economics
University of Florida
https://people.clas.ufl.edu/skostyshak/

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel