R 高级错误处理

R 高级错误处理,r,error-handling,conditional-statements,application-restart,R,Error Handling,Conditional Statements,Application Restart,我最近摆了个姿势,谢天谢地有人指着with restarts() 实际问题 simpleCondition()的推荐用法是什么?以前从未使用过它,但我认为它可能有助于设计实际上是“真实”条件的自定义错误和警告。它是否可以用于构建特定条件的数据库,这些特定条件下可以使用特定的处理程序 是否有一种方法可以“冻结”整个R工作区的特定状态,并返回到该状态以在特定点重新启动计算?我知道save.image(),但是现在,它没有存储搜索路径的“状态”(search()或searchpath()) 有兴趣的

我最近摆了个姿势,谢天谢地有人指着
with restarts()

实际问题
  • simpleCondition()
    的推荐用法是什么?以前从未使用过它,但我认为它可能有助于设计实际上是“真实”条件的自定义错误和警告。它是否可以用于构建特定条件的数据库,这些特定条件下可以使用特定的处理程序
  • 是否有一种方法可以“冻结”整个R工作区的特定状态,并返回到该状态以在特定点重新启动计算?我知道
    save.image()
    ,但是现在,它没有存储搜索路径的“状态”(
    search()
    searchpath()

  • 有兴趣的人 两个代码示例

  • 我当前使用的
    随此重新启动的说明
  • 尝试定义“自定义条件”
  • 如有任何关于如何做得更好的意见/建议,我将不胜感激;-)

    例1
    require(“预测”)
    autoArimaFailsafepost引用了R的条件处理的灵感

    对于1,我认为
    simpleCondition
    说明了如何构造自定义条件,例如

     myCondition <-
        function(message, call=NULL, type=c("overflow", "underflow", "zero"))
    {
        type <- match.arg(type)             # only allowed types past here
        class <- c(type, "my", "condition")
        structure(list(message = as.character(message), call = call), 
            class = class)
    }
    
    这些是S3类,因此可以具有线性层次结构--
    bad
    bard
    都是
    error
    的子类

    myError <-
        function(message, call=NULL, type=c("bad", "worse"))
    {
        type <- match.arg(type)
        class <- c(type, "error", "condition")
        structure(list(message=as.character(message), call=call),
                  class=class)
    }
    
    使用
    withCallingHandlers
    我们有机会点击多个处理程序,只要我们不调用重启

    withCallingHandlers({
        stop(myError("oops", type="bad"))
    }, bad = function(e) {                             # here...
        message("bad error: ", conditionMessage(e))
    }, worse = function(e) {
        message("worse error: ", conditionMessage(e))
    }, error=function(e) {                             # ...and here...
        message("error: ", conditionMessage(e))
    })                                                 # ...and top-level 'error'
    
    withCallingHandlers({
        x <- 1
        warning(myError("oops", type="bad"))
        "OK"
    }, bad = function(e) {                     # here, but continue at the restart
        message("bad warning: ", conditionMessage(e))
        invokeRestart("muffleWarning")
    }, worse = function(e) {
        message("worse warning: ", conditionMessage(e))
    })
    
    withCallingHandler({
    停止(myError(“oops”,type=“bad”))
    },bad=函数(e){#这里。。。
    消息(“错误:,条件消息(e))
    },更差=函数(e){
    消息(“更糟的错误:”,条件消息(e))
    },error=function(e){#…这里。。。
    消息(“错误:,条件消息(e))
    })#…和顶级“错误”
    使用呼叫处理程序({
    
    哇,真棒的回答!非常感谢
    > myCondition("oops")
    <overflow: oops>
    > myCondition("oops", type="underflow")
    <underflow: oops>
    
    xx <- tryCatch({
        signalCondition(myCondition("oops", type="underflow"))
    }, underflow=function(e) {
        message("underflow: ", conditionMessage(e))
        NA # return value, assigned to xx
    })
    
    myError <-
        function(message, call=NULL, type=c("bad", "worse"))
    {
        type <- match.arg(type)
        class <- c(type, "error", "condition")
        structure(list(message=as.character(message), call=call),
                  class=class)
    }
    
    tryCatch({
        stop(myError("oops", type="worse"))
    }, bad = function(e) {
        message("bad error: ", conditionMessage(e))
    }, worse = function(e) {
        message("worse error: ", conditionMessage(e))  # here's where we end up
    }, error=function(e) {
        message("error: ", conditionMessage(e))
    })
    
    withCallingHandlers({
        stop(myError("oops", type="bad"))
    }, bad = function(e) {                             # here...
        message("bad error: ", conditionMessage(e))
    }, worse = function(e) {
        message("worse error: ", conditionMessage(e))
    }, error=function(e) {                             # ...and here...
        message("error: ", conditionMessage(e))
    })                                                 # ...and top-level 'error'
    
    withCallingHandlers({
        x <- 1
        warning(myError("oops", type="bad"))
        "OK"
    }, bad = function(e) {                     # here, but continue at the restart
        message("bad warning: ", conditionMessage(e))
        invokeRestart("muffleWarning")
    }, worse = function(e) {
        message("worse warning: ", conditionMessage(e))
    })