I have a function that generates an unwanted warning, but keeps the value.
f <- function(a) {
if (a > 1) {
warning("an uninformative warning")
}
a
}
g1 <- function(b) {
withCallingHandlers(
x <-f(b),
warning = function(w) {
warning("A more informative warning")
})
x
}
g1(2)
#> Warning in (function (w) : A more informative warning
#> Warning in f(b): an uninformative warning
#> [1] 2
Created on 2023-12-12 with reprex v2.0.2
Unfortunately, this thows 2 warnings.
With tryCatch() x is not kept. and with withCallingHandlers(), both warnings are thrown.
You can get your
warninghandler to emit its own warning, then invoke themuffleWarningrestart:Testing: