如何捕捉S4方法选择期间发送的警告
使用withCallingHandlers ()无法捕获S4通用函数的评估参数时生成的警告 。
withCallingHandlers的正常行为的示例:
### simple function that sends a warning
send_warning <- function() {
warning('send_warning')
}
send_warning()
# Warning message:
# In send_warning() : send_warning
### the warning can be caught by withCallingHandlers
withCallingHandlers(send_warning(), warning = function(w)
{ stop('got warning:', w) })
# Error in (function (w) :
# got warning:simpleWarning in send_warning(): send_warning
现在,让我们在S4发布期间发出警告:
### simplest method ever
setGeneric('my_method', function(x) standardGeneric('my_method') )
setMethod('my_method', 'ANY', function(x) str(x) )
### call it with an argument that produces a warning: seems to work
my_method(warning('argh'))
# chr "argh"
# Warning message:
# argh
### !!! BUT it is not caught !!!!!!!!!!!!!!!!!!:
withCallingHandlers(my_method(warning('argh')), warning = function(w)
{ stop('got warning:', w) })
# chr "argh"
# Warning message:
# argh
最后一个例子显示在S4方法调用期间不会发生这种情况:
setGeneric('my_method2', function(x) standardGeneric('my_method2') )
setMethod('my_method2', 'ANY', function(x) warning('my_method2') )
my_method2()
# Warning message:
# In my_method2() : my_method2
### warning is caught
withCallingHandlers(my_method2(), warning = function(w)
{ stop('got warning:', w) })
# Error in (function (w) :
# got warning:simpleWarning in my_method2(): my_method2
据我所知,在S4派遣期间发出的警告似乎有一种特殊的行为。 我想知道为什么以及如何捕捉它们。
我原来的猜测是,这是因为方法派遣马夫警告
f = function(x) withCallingHandlers(x, warning=function(w) {
cat('muffledn')
invokeRestart("muffleWarning")
})
接着
> withCallingHandlers(f(warning("f")), warning=function(w) message("caught"))
muffled
深入挖掘,实际的警告调用在此C堆栈跟踪中
#0 do_warning (call=0x3d4ad50, op=0x9c4bf0, args=0x45b9968, rho=0x45b9c40) at /home/mtmorgan/src/R-devel/src/main/errors.c:1140
#1 0x00000000004b4198 in bcEval (body=<optimized out>, rho=<optimized out>, useCache=<optimized out>) at /home/mtmorgan/src/R-devel/src/main/eval.c:4700
#2 0x00000000004bff40 in Rf_eval (e=0x3d45618, rho=0x45b9c40) at /home/mtmorgan/src/R-devel/src/main/eval.c:554
#3 0x00000000004c4e4d in Rf_applyClosure (call=0x45b8630, op=0x3d45730, arglist=<optimized out>, rho=0x9e7638, suppliedenv=0x9e7670) at /home/mtmorgan/src/R-devel/src/main/eval.c:1033
#4 0x00000000004c0005 in Rf_eval (e=0x45b8630, rho=0x9e7638) at /home/mtmorgan/src/R-devel/src/main/eval.c:670
#5 0x00000000004c0695 in forcePromise (e=0x45b9410) at /home/mtmorgan/src/R-devel/src/main/eval.c:458
#6 0x00000000004c0462 in Rf_eval (e=0xa1d338, rho=0x45b9368) at /home/mtmorgan/src/R-devel/src/main/eval.c:577
#7 0x000000000046fbfb in protectedEval (d=0x7fffffffc7f0) at /home/mtmorgan/src/R-devel/src/main/context.c:750
#8 0x0000000000470d48 in R_ToplevelExec (fun=0x46fbe0 <protectedEval>, data=0x7fffffffc7f0) at /home/mtmorgan/src/R-devel/src/main/context.c:705
#9 0x0000000000470de7 in R_tryEval (e=<optimized out>, env=<optimized out>, ErrorOccurred=0x7fffffffc89c) at /home/mtmorgan/src/R-devel/src/main/context.c:764
#10 0x0000000000470e26 in R_tryEvalSilent (e=<optimized out>, env=<optimized out>, ErrorOccurred=<optimized out>) at /home/mtmorgan/src/R-devel/src/main/context.c:787
#11 0x00007ffff49230b9 in R_dispatchGeneric (fname=0x44b37e8, ev=0x45b9368, fdef=0x45b92f8) at /home/mtmorgan/src/R-devel/src/library/methods/src/methods_list_dispatch.c:993
#12 0x00000000004f5337 in do_standardGeneric (call=<optimized out>, op=<optimized out>, args=<optimized out>, env=0x45b9368) at /home/mtmorgan/src/R-devel/src/main/objects.c:1167
....
在R_dispatchGeneric
方法调度的C代码中,通过评估它试图找出参数的类
989 if(arg_sym == R_dots) {
990 thisClass = dots_class(ev, &check_err);
991 }
992 else {
993 PROTECT(arg = R_tryEvalSilent(arg_sym, ev, &check_err));
994 if(!check_err)
995 thisClass = R_data_class(arg, TRUE);
996 UNPROTECT(1); /* for arg */
997 }
这似乎是为了实现一个C级错误处理程序:
998 if(check_err)
999 error(_("error in evaluating the argument '%s' in selecting a method for function '%s': %s"),
1000 CHAR(PRINTNAME(arg_sym)),CHAR(asChar(fname)),
1001 R_curErrorBuf());
Rf_tryEvalSilent
在顶层进行评估,就像在没有建立调用处理程序的命令提示符下一样; 你可以在处理器堆栈设置为NULL的C代码中看到这一点
686 Rboolean R_ToplevelExec(void (*fun)(void *), void *data)
687 {
688 RCNTXT thiscontext;
689 RCNTXT * volatile saveToplevelContext;
690 volatile SEXP topExp, oldHStack;
691 Rboolean result;
692
693
694 PROTECT(topExp = R_CurrentExpr);
695 PROTECT(oldHStack = R_HandlerStack);
696 R_HandlerStack = R_NilValue;
697 saveToplevelContext = R_ToplevelContext;
由于参数评估是在没有处理程序的情况下开始的,因此可能会有解决方法
my_method(withCallingHandlers(warning('arrgh'), warning=function(w) ...))
但这可能不实际。
链接地址: http://www.djcxy.com/p/17541.html