I am in the midst of trying to improve error and warning messages in a
complex package.  I find myself wanting to modify R's default behavior when
printing warnings.  Specifically, it frequently happens for me that the
call that is printed with the warning message is not informative.  When the
warning arises in R code, I have the ability to suppress the printing of
the call by setting 'call.=FALSE'.  However, when the warning arises in C
code (via the 'warning()' function), I have no such option.

'tryCatch' and 'withCallingHandlers' are of no use (or at least, if they
are, I haven't figured out how).

Consider the following simple example:

Contents of file 'example.c':
--------------------------------------------------
#include <R.h>

void example (int *x) {
  if (*x>0)
    error("ouch");
  else if (*x<0) {
    warning("duck!");
    *x = 22;
  } else {
    *x = 11;
  }
}
--------------------------------------------------

Compile and load the C code:
--------------------------------------------------
system2(R.home("bin/R"),args=c("CMD","SHLIB","example.c"))
dyn.load(paste0("example",.Platform$dynlib.ext))
--------------------------------------------------

The function 'f' calls 'example' as defined above.  On an error, it
modifies the error message using 'tryCatch'.
--------------------------------------------------
f <- function (x) {
    y <- tryCatch(
        .C("example",x=as.integer(x))$x,
        error = function (e) {
            stop("in f: ",conditionMessage(e),call.=FALSE)
        }
    )
    y
}
--------------------------------------------------

Get an error:

> f(1)
Error: in f: ouch!

Get a warning:

> f(-1)
[1] 22
Warning message:
In doTryCatch(return(expr), name, parentenv, handler) : duck!

It's the fact that 'doTryCatch(return(expr), name, parentenv, handler)' is
meaningless to the user that bothers me.  How can I modify the warning?

Thanks in advance for any insight.

-- 
Professor Aaron A. King
Ecology & Evolutionary Biology
Mathematics
Center for the Study of Complex Systems
University of Michigan
GPG Public Key: 0x15780975

        [[alternative HTML version deleted]]

______________________________________________
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel

Reply via email to