R:创建一个自定义错误消息,为包中的函数提供参数值 [英] R: Creating a custom error message giving argument values for a function in a package

查看:121
本文介绍了R:创建一个自定义错误消息,为包中的函数提供参数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设有一组函数,从一个不是由我写的软件包中提取,我想分配给一个特殊的错误行为。我目前关心的是dplyr中的_impl函数族。以mutate_impl为例。当我从mutate中得到一个错误时,traceback几乎总是将我引向mutate_impl,但它通常是调用堆栈的一种方式 - 我已经看到多达15次调用mutate的调用。所以我现在想知道的是,mutate_impl的参数通常与我最初提供给mutate的参数有关(或者认为我做过)。

所以,这个代码在很多方面可能都是错误的 - 当然它不起作用 - 但我希望它至少有助于表达我的意图。这个想法是我可以把它包装到mutate_impl中,如果它产生一个错误,它会保存错误信息和参数的描述,并将它们作为列表返回。



<$ p code $ str_impl< - function(f){tryCatch(f,error = function(c){
msg< - conditionMessage(c)
args< - capture .output(str(as.list(match.call(call(f)))))
list(message = msg,arguments = args)
}
assign(str_impl(mutate_impl) ,.GlobalEnv)

虽然这仍然不符合我真正想要的,因为即使没有约束的工作代码,我无法弄清楚如何生成一个草稿,我真正想要的是能够识别出一个函数或函数列表,我想这个行为出错,然后在出现错误时发生无论在哪里调用函数,我都想不出有什么办法可以在dplyr软件包环境中重写函数,而不用重新编写函数,最后的分配给全局环境应该是让错误对象回到我能找到它的地方,即使对mutate_impl的调用发生在某个不可访问的地方,就像在错误发生后不再存在的环境中一样。

解决方案

可能是实现您想要的最好方式是通过 trace 功能。当然值得阅读关于 trace 的帮助,但这里是一个工作示例:

  
trace(mutate_impl,exit = quote({
if(class(returnValue())[1] ==NULL){
cat(df\\\

print(head(df))
cat(\\\
\\\
dots\\\

print(dots)
} else {
#没问题,没事做
}
}),其中= mutate,print = FALSE)

#ok
xx< - mtcars%>%mutate(gear = gear * 2)
#not ok,extra output
xx< - mtcars%>%mutate(gear = hi * 2)

根据您的特定需求调整它应该相当简单,例如如果您想登录到文件,请改为:

  trace(mutate_impl,exit = quote ({
if(class(returnValue())[1] ==NULL){
sink(error.log)
cat(df\\\

print(head(df))
cat(\\\
\\\
dots\\\

print(dots)
sink()
} else {
#没问题,没事做
}
}),其中= mutate,print = FALSE)


Suppose there is a set of functions, drawn from a package not written by me, that I want to assign to a special behavior on error. My current concern is with the _impl family of functions in dplyr. Take mutate_impl, for example. When I get an error from mutate, traceback almost always leads me to mutate_impl, but it is usually a ways up the call stack -- I have seen it be as many as 15 calls from the call to mutate. So what I want to know at that point is typically how the arguments to mutate_impl relate to the arguments I originally supplied to mutate (or think I did).

So, this code is probably wrong in too many ways to count -- certainly it does not work -- but I hope it at least helps to express my intent. The idea is that I could wrap it around mutate_impl, and if it produces an error, it saves the error message and a description of the arguments and returns them as a list

str_impl  <- function(f){tryCatch(f, error = function(c) {
    msg <- conditionMessage(c)
    args <- capture.output(str(as.list(match.call(call(f)))))
    list(message =  msg, arguments = args)
}
assign(str_impl(mutate_impl), .GlobalEnv)

Although, this still falls short of what I really want, because even without the constraint of working code, I could not figure out how to produce a draft. What I really want is to be able to identify a function or list of functions that I want to have this behavior on error, and then have it occur on error whenever and wherever that function is called. I could not think of any way to even start to do that without rewriting functions in the dplyr package environment, which struck me as a really bad idea.

The final assignment to the global environment is supposed to get the error object back to somewhere I can find it, even if the call to mutate_impl happens somewhere inaccessible, like in an environment that ceases to exist after the error.

解决方案

Probably the best way of achieving what you want is via the trace functionality. It's surely worth reading the help about trace, but here is a working example:

library(dplyr)

trace("mutate_impl", exit = quote({
  if (class(returnValue())[1]=="NULL") {
    cat("df\n")
    print(head(df))
    cat("\n\ndots\n")
    print(dots)
  } else {
    # no problem, nothing to do
  }
}), where = mutate, print = FALSE)

# ok
xx <- mtcars %>% mutate(gear = gear * 2)
# not ok, extra output
xx <- mtcars %>% mutate(gear = hi * 2)

It should be fairly simple to adjust this to your specific needs, e.g. if you want to log to a file instead:

trace("mutate_impl", exit = quote({
  if (class(returnValue())[1]=="NULL") {
    sink("error.log")
    cat("df\n")
    print(head(df))
    cat("\n\ndots\n")
    print(dots)
    sink()
  } else {
    # no problem, nothing to do
  }
}), where = mutate, print = FALSE)

这篇关于R:创建一个自定义错误消息,为包中的函数提供参数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆