I am trying to signal a custom condition in R using a condition object. However, it seems that I cannot set the immediate. argument to TRUE, nor can I alter any other argument to the stop, warning, etc functions. Is there a way to use condition objects but still retain the other arguments of stop, warning, and message ?
MWE:
Warning in perhaps the "usual" way works as expected, meaning that the warnings occur at each iteration. This is what I desire.
# Works as expected
for(i in 1:20){warning("A warning", immediate.=T) ; Sys.sleep(.1)}
>Warning: A warning
>Warning: A warning
>Warning: A warning
>Warning: A warning
>Warning: A warning
... [truncated]
In contrast, when using a condition object below, the warnings are collected and then we're told that there are 20 of them at the end:
Attempt 1
# Does not signal warning immediately
cond = structure(list(message="A warning", call=NULL, immediate.=T), class=c("warning", "condition"))
for(i in 1:20){ warning(cond); Sys.sleep(.1) }
>There were 20 warnings (use warnings() to see them)
Attempt 2 -- Setting the argument manually
for(i in 1:20) {warning(cond, immediate.=T); Sys.sleep(.1)}
>additional arguments ignored in warning()
>additional arguments ignored in warning()
>...[truncated]
>There were 20 warnings (use warnings() to see them)
Attempt 3 -- Deleting the immediate.=T argument from the cond object, and retrying (2)
cond = structure(list(message="A warning", call=NULL), class=c("warning", "condition"))
for(i in 1:20) {warning(cond, immediate.=T); Sys.sleep(.1)}
>additional arguments ignored in warning()
>additional arguments ignored in warning()
>...[truncated]
>There were 20 warnings (use warnings() to see them)
Is there a way to use condition objects but still retain the other arguments of stop, warning, and message ?