[R-pkg-devel] Testing for a Specific R Error
Duncan Murdoch
murdoch.duncan at gmail.com
Sat Dec 2 15:43:13 CET 2017
On 02/12/2017 9:23 AM, Dirk Eddelbuettel wrote:
>
> On 2 December 2017 at 08:38, Bill Denney wrote:
> | Hi,
> |
> | I got a message last night that some of the tests in the PKNCA package do not follow best practices. ("Do not test the exact format of R messages (from R itself or from other packages): They change, and they can be translated.") Specifically, I test to ensure that an error is generated when a class cannot be coerced into a data.frame: https://cran.r-project.org/web/checks/check_results_PKNCA.html
> |
> | I want to ensure that I'm getting an error that the variable cannot be coerced into a data.frame.
> |
> | What is the best practice to ensure that I'm getting a specific R error (about coercion) without testing the formatting of the error text?
>
> To me this suggest testing the _condition_ triggering the error message, as
> opposed to testing for the displayed text of the error message.
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
More information about the R-package-devel
mailing list