Dear all,

I agree that with Kirsten that adding the options(warnNonexistDollar=TRUE)
would be useful to catch problems outside stopifnot().

However, is it not much better to use indexing through `[`, which throws an
error for nonexistent columns?
That solves the problem of silently glossing over wrongly-spelled names and
the problem of partial matching (which it does not use).

```
dat <- data.frame(column1=1:3,column2=NA)
stopifnot(!any(is.na(dat[, "coluumn2"])))
# Error in `[.data.frame`(dat, , "coluumn2") : undefined columns selected
stopifnot(sum(dat[, "coluumn1"]) < 2)
# Error in `[.data.frame`(dat, , "coluumn1") : undefined columns selected
```

Throwing an error of course makes this approach unsuitable to check if a
column is present while simultaneously performing the assignment in the way
Martin's example did.
But first checking the presence of the name would work fine, even though it
is a bit slower:

```
if(comp %in% names(mylist)) {
    .....
    <work with> mylist["comp"]
    .....
} else {
    ## work without `mylist["comp"]  `, in some cases just
    stop("component `comp` does not exist -- cannot do xyz")
}
```

Kind regards,
Jesse

Op vr 6 nov 2026 om 19:41 schreef Kristen Kelly via R-devel <
[email protected]>:

> Thanks Martin!
>
> I definitely think the option for stopifnot() would help with the the
> first example I gave.
>
> But the problem runs deeper than just stopifnot(). In the second example,
> the problem is that sum(dat$coluumn1) is 0. So stopifnot() is receiving a
> perfectly legitimate test that 0 < 2. This would also be a problem outside
> of stopifnot(), eg. someone might assign x <- sum(dat$coluum1) and then try
> to use x for further calculations.
>
> There are probably also other R functions besides stopifnot() and sum()
> that also silently do weird things with no warning when $ unexpectedly
> returns NULL due to typos.
>
> Personally, I would rather see the warnings for mistyped/non-existent
> column names with $ than not, even if it meant seeing some extra warnings.
> Better that than having typos cause incorrect results in analyses with no
> warning. Having it be a settable option (similar to warnPartialMatchDollar)
> would let each user make that choice for themselves.
>
> Kristen
>
>
>
> From: Martin Maechler <[email protected]>
> Date: Friday, November 6, 2026 at 11:20 AM
> To: Kristen Kelly <[email protected]>
> Cc: R Development List <[email protected]>
> Subject: stopifnot(<empty>) was "Potential feature request: Option to warn
> ... with data.frames and $"
> [External email - use caution]
>
>
> >>>>> Kristen Kelly via R-devel
> >>>>>     on Fri, 6 Nov 2026 01:43:19 +0000 writes:
>
>     > Hello,
>     > I hope I am writing to the correct list. I am hoping to make a
> feature request for R, and I found a guide that says to write to r-devel
> first to check if there is support for the idea.
>
>     > The feature I want to suggest is an option similar to
> options(warnPartialMatchDollar=TRUE), but for cases where rather than being
> a partial match, the variable name is misspelled or simply doesn�t exist
> and attempting to use it produces NULL.
>
>     > This behavior of $ can sometimes lead to unexpected outcomes, as
> demonstrated below:
>
>     > dat <- data.frame(column1=1:3,column2=NA)
>     > stopifnot(!any(is.na(dat$coluumn2)))
>     > stopifnot(sum(dat$coluumn1) < 2)
>
>     > In both cases, the data.frame returning NULL with a misspelled
> column name leads to the check in the stopifnot() silently succeeding with
> no warning that anything might be wrong.
>
>     > I know there are reasons why $ returns NULL, and I�m not suggesting
> changing that. Just possibly adding an option, something like
> options(warnNonexistDollar=TRUE), that people could turn on if they would
> find it helpful to get a warning in cases like the example above.
>
>     > Is this something that�s likely to have enough support to be worth
> submitting as a feature request?
>
>     > Thank you,
>     > Kristen
>
> Thank you, Kirsten,
>
> for opening an interesting issue.
> Personally I'm guessing that such a warning may not be very
> useful as I expect it to be triggered too often.
> Notably it has been a "trick" already in S, S-plus, for a really
> fast test if a list component to be present,
> to have something like
>
>    if(!is.null(cm <- mylist$comp)) {
>      .....
>      <work with> `cm`
>      .....
>    } else {
>      ## work without `cm`, in some cases just
>      stop("component `comp` does not exist -- cannot do xyz")
>    }
>
> *HOWEVER*, I think the problem you really may want to solve
> is the use inside stopifnot():
>
> The "problem" is that    all( logical(0) )  is TRUE
> and _should_ be {all elements of the empty set are TRUE .. etc},
> i.e., the "problem" is the specification of the  stopifnot()
> function --- I added to R a *long* time ago, that it can and
> often should be used "vectorized", i.e.,
>
>   stopifnot( x == y )
>
> should be 'fine' (in simple vector/matrix/array cases)  when x
> and y have the same lengths and all their corresponding elements
> x[i], y[i]  are equal.
>
> Hence stopifnot() uses internally code which may be simplified to
> all(x == y).
>
> Really experienced R users (say 0.025% of all ;-) of course are
> aware of that .. but it may still be considered "dangerous"
> behaviour, a really bad case being
>
>     stopifnot(dim(x) == dim(y))
>
> to check if two matrices (or arrays) are compatible.  If one of
> x or y is accidentally a vector instead of a matrix, the above
> never signals the desired error.
>
> Hence, for many years, I have mused about, but never posted (nor
> even discussed IIRC within the R core team ..)
> an official proposal to eventually *change* stopifnot's default
> behavior, and to eventually *warn* when its call to all(.) would be
> applied to
> an object of length 0.
> Again this makes too many uses unuseful, so I have had (starting from
> on April 13, 2020) a new argument to stopifnot, which *indeed*
> is tweakable by a new option, as you can see:
>
> stopifnot <- function(..., exprs, exprObject, local = TRUE,
>                       allow.logical0 =
> getOption("stopifnot_allow.logical0", TRUE))
> { ## allow.logical0=TRUE: compatible w/ R <= 4.6.x; to become FALSE; at
> first in '--as-cran' !
>
>     ..............................
>     ..............................
>     <.... some 'allow.logical0' hack in the branch for
>     stopifnot(exprs = { ......
>     } )
>     ....>
>     ..............................
>     ..............................
>
>     allTRUE <- if(allow.logical0)
>                    function(x)              is.logical(x) && !anyNA(x) &&
> all(x)
>                else
>                    function(x) length(x) && is.logical(x) && !anyNA(x) &&
> all(x)
>
>     ... and then
>
>     for (i in seq_len(n)) {
>         r <- ...elt(i)
>         if(!allTRUE(r)) {
>            ............
>            ............
>            stop(..............)
>            .........
>        }
>     }
>     invisible()
> } # end { stopifnot() definition }
>
> Once I've started using this  in my "hacked" version of R-devel,
> *and* set the
>
>    options(stopifnot_allow.logical0 = FALSE)
>
> I did need to add manually   `allow.logical0 = TRUE`
> to *some* stopifnot() calls in my R code (in my packages and scripts)
> and to R-core's code in base R {i.e., in the above "hacked"
> branch of R-devel}.
>
> I vaguely remember (but am not sure, not remembering details)
> that the updated stopifnot() -- without setting the new option
> still had some rare hickups, but I really don't remember and
> also somewhat doubt my vague memory...
>
> Maybe I should open an R bugzilla "wishlist" for this,
> with my full current patch for such an enhanced stopifnot().
>
> In any case, your issue would be solved either by explicitly adding
>   allow.logical0 = FALSE
> to your stopifnot() calls involving  ` <datafr>$<comp> `
> or by setting the above options(stopif... = FALSE)
>
> What do others think - about such an option for stopifnot()?
>
> With a repeated thank you for raising the issue,
> Martin
>
>
> --
> Martin Mächler
> R Core team and ETH Zurich
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [email protected] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

        [[alternative HTML version deleted]]

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

Reply via email to