当需要清除状态时,如何优雅地处理R中的错误?

编程入门 行业动态 更新时间:2024-10-26 06:29:38
本文介绍了当需要清除状态时,如何优雅地处理R中的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

处理R中的错误的最佳方法是什么?我希望既可以从脚本的最终用户那里提取堆栈跟踪信息,又可以清理我可能正在使用的所有临时变量和状态。

What is the best was to handle errors in R? I want to be able to both abstract away stack traces from the end user of my script, as well as clean up any temporary variables and state i may be working with.

因此,我想我的问题有两个方面:

So i suppose my question is two fold:

  • 最重要的是,在处理错误时如何正确处理清理状态?
  • 如何在不吐出内部东西的情况下从R的错误消息中提取有用的好信息?
  • 此刻,我有一些类似的东西,但是(1)似乎太糟糕了,(2)仍然给了我可怕的错误消息。

    At the moment, I have something which looks like this, but (1) it seems horribly inelegant, and (2) still gives me horrible error messages.

    # Create some temporary working state and variables to clean up file <- "somefile" working.dir <- getwd() setwd("../") # Go somewhere else saf.default <- getOption("stringsAsFactors") options(stringsAsFactors = FALSE) # Now lets try to open the file, but it doesn't work (for whatever reason) # I want to clean up the state, stop, and wrap the error string with a nicer # message tryCatch({ # Need to embed the tryCatch because we still need some of the state variables # for the error message tryCatch({ f <- read.table(file) }, error = function(err.msg) { # Can't clean up here, we still need the `file variable! stop("Could not open file '", file, "' with error message:\n", print(err.msg), call.=FALSE) }) }, error = function(err.msg) { # Now we can clean up state setwd(working.dir) options(stringsAsFactors = saf.default) rm(file, working.dir, saf.default, envir=globalenv()) # This also seems awful? stop(print(err.msg), call.=FALSE) }) # Do more stuff, get more state, handle errors, then clean up. # I.e can't use `finally` in previous tryCatch!

    由此产生的错误消息仍然是许多丑陋的内部元素:

    The error message from this comes out as, still lots of ugly internals:

    # <simpleError in file(file, "rt"): cannot open the connection> # <simpleError: Could not open file 'somefile' with error message: # Error in file(file, "rt"): cannot open the connection > # Error: Could not open file 'somefile' with error message: # Error in file(file, "rt"): cannot open the connection # In addition: Warning messages: # 1: In file(file, "rt") : # cannot open file 'somefile': No such file or directory # 2: In stop(print(err.msg), call. = FALSE) : # additional arguments ignored in stop() >

    推荐答案

    我会将任何状态更改代码隔离到自己的代码中函数,并使用 on.exit 。这样可以保证无论是否发生错误,都将进行清理。

    I would isolate any state-changing code into its own function, and use on.exit. This guarantees that cleanup will happen, no matter if an error occurs.

    readFile <- function(.....) { on.exit({ setwd(cur.dir) options(stringsAsFactors=saf) }) cur.dir <- getwd() saf <- getOption("stringsAsFactors") setwd("new/dir") options(stringsAsFactors=FALSE) .... }

    更多推荐

    当需要清除状态时,如何优雅地处理R中的错误?

    本文发布于:2023-11-03 16:38:15,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1555600.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:优雅   状态   错误

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!