Skip to content
Prev 2145 / 12125 Next

[R-pkg-devel] Testing for a Specific R Error

On 02/12/2017 9:23 AM, Dirk Eddelbuettel wrote:
Isn't that what Bill suggested in the line below?

 > | The only solution that immediately occurs to me is to wrap the 
coercion in a tryCatch and give my own error.  But, then were the R 
error translated, the users of my package would lose the benefit of 
translation.

R does have a system for giving language-independent information about 
errors and warnings:  see the ?signalCondition help page. 
Unfortunately, support for this at the C level is missing, so base 
functions generally don't use it.  You only get something of class 
c("simpleError", "error", "condition").

I don't think there's anything better than Bill's solution, though I 
imagine it is possible to ask for translation of the message.  For 
example, sqrt(-1) currently gives a warning with English message "NaNs 
produced".

I can ask to translate that into the current session language using

gettext("NaNs produced", domain = "R")

Figuring out the right thing for "domain" is likely a little painful: 
it depends on which package produced the message, and how.

I don't know if CRAN tests would complain if you tested for equality 
between a warning message and the result of gettext():  it's still true 
that if the English warning changed, the test would fail.

Duncan Murdoch