JJ <josh8912 <at> yahoo.com> writes:
:
: Hello:
: I recently asked the list a question regarding
: warning.expression. This is a different statement of
: the problem.
:
: I am doing a large simulation experiment using nlme
: and most of the data realizations run fine. The
: simulations stall, however, on a few particularly
: noisy data sets. What happens is that the nlme
: function initially calls the nlm function and some
: code in there generates an endless number of warning
: messages. These are: "Warning: Singular precision
: matrix in level ...". Becuse of this, my simulation
: is caught in an endless loop and after quite a while R
: crashes.
:
: I do use try(nlme(...)), so if I could find a way to
: flag these warning and make them into errors, such as
: by calling stop(), then my code could move on to the
: next realizaton of the simulation. Some warnings are
: produced that are not endless or otherwise
: problematic, so I do not want to cause an error for
: every warning.
:
: Does anyone have any ideas as to how to identify
: warning messages and call stop() or otherwise induce
: an error if the warnings are of a particular type?
See ?withCallingHandlers . Here is an example which raises an
error for an "X" warning but not other sorts of warnings.
f <- function(x) if (x$message == "X") stop("X")
withCallingHandlers({
print(10)
warning("Y") # this warning does not result in stop
print(20)
warning("X") # this warning results in stop
print(30)
}, warning = f)