R allows you to define a condition handler
x <- tryCatch({
warning("oops")
}, warning=function(w) {
## do something about the warning, maybe return 'NA'
message("handling warning: ", conditionMessage(w))
NA
})
which results in
handling warning: oops
> x
[1] NA
Execution continues after tryCatch; you could decide to end by converting your warning to an error
x <- tryCatch({
warning("oops")
}, warning=function(w) {
stop("converted from warning: ", conditionMessage(w))
})
or handle the condition gracefully (continuing evaluation after the warning call)
withCallingHandlers({
warning("oops")
1
}, warning=function(w) {
message("handled warning: ", conditionMessage(w))
invokeRestart("muffleWarning")
})
which prints
handled warning: oops
[1] 1