[Rd] [External] DOCS: Exactly when, in the signaling process, is option 'warn' applied?

iuke-tier@ey m@iii@g oii uiow@@edu iuke-tier@ey m@iii@g oii uiow@@edu
Thu Nov 18 19:22:34 CET 2021

On Thu, 18 Nov 2021, Henrik Bengtsson wrote:

> Hi,
> the following question sprung out of a package settings option warn=-1
> to silence warnings, but those warnings were still caught by
> withCallingHandlers(..., warning), which the package author did not
> anticipate. The package has been updated to use suppressWarnings()
> instead, but as I see a lot of packages on CRAN [1] use
> options(warn=-1) to temporarily silence warnings, I wanted to bring
> this one up. Even base R itself [2] does this, e.g.
> utils::assignInMyNamespace().
> Exactly when is the value of 'warn' options used when calling warning("boom")?

In the default handler; it doesn't affect signaling.

Much of the documentation pre-dates the condition system; happy to
consider patches.



> I think the docs, including ?options, would benefit from clarifying
> that. To the best of my understanding, it should also mention that
> options 'warn' is meant to be used by end-users, and not in package
> code where suppressWarnings() should be used.
> To clarify, if we do:
>> options(warn = -1)
>> tryCatch(warning("boom"), warning = function(w) stop("Caught warning: ", conditionMessage(w), call. = FALSE))
> Error: Caught warning: boom
> we see that the warning is indeed signaled.  However, in Section '8.2
> warning' of the 'R Language Definition' [3], we can read:
> "The function `warning` takes a single argument that is a character
> string. The behaviour of a call to `warning` depends on the value of
> the option `"warn"`. If `"warn"` is negative warnings are ignored.
> [...]"
> The way this is written, it may suggest that warnings are
> ignored/silences already early on when calling warning(), but the
> above example shows that that is not the case.
> From the same section, we can also read:
> "[...] If it is zero, they are stored and printed after the top-level
> function has completed. [...]"
> which may hint at the 'warn' option is applied only when a warning
> condition is allowed to "bubble up" all the way to the top level.
> (FWIW, this is how always though it worked, but it's only now I looked
> into the docs and see it's ambiguous on this).
> /Henrik
> [1] https://github.com/search?q=org%3Acran+language%3Ar+R%2F+in%3Afile%2Cpath+options+warn+%22-1%22&type=Code
> [2] https://github.com/wch/r-source/blob/0a31ab2d1df247a4289efca5a235dc45b511d04a/src/library/utils/R/objects.R#L402-L405
> [3] https://cran.r-project.org/doc/manuals/R-lang.html#warning
> ______________________________________________
> R-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

Luke Tierney
Ralph E. Wareham Professor of Mathematical Sciences
University of Iowa                  Phone:             319-335-3386
Department of Statistics and        Fax:               319-335-3017
    Actuarial Science
241 Schaeffer Hall                  email:   luke-tierney using uiowa.edu
Iowa City, IA 52242                 WWW:  http://www.stat.uiowa.edu

More information about the R-devel mailing list