【问题标题】:Suppress error message when using fitdist() from the fitdistrplus package使用 fitdistrplus 包中的 fitdist() 时抑制错误消息
【发布时间】:2018-12-10 10:57:19
【问题描述】:

我正在使用fitdistrplus 包中的某些函数作为我正在创建的包的一部分。

我试图阻止在运行函数时在控制台中显示任何错误消息,而是希望将错误消息记录在我正在创建的错误日志中。

在大多数情况下,使用tryCatch() 让我能够实现这一点。

但特别是对于 fitdist() 函数,即使正在将消息写入错误日志(意味着 tryCatch() 表达式正在运行),我也无法禁止在控制台中打印错误消息。

我在下面的代码中复制了我的问题。

library(fitdistrplus)

file.create("error_log.txt")

func_desc<-function(x){
  tryCatch({
    descdist(data = x)
  },error = function(e){
    write(x = paste(Sys.time(),"Error in func_desc :",e$message,sep = " "),file = "error_log.txt",append = T,sep = "\n")
  })
}

func_fit<-function(x,dist){
  tryCatch({
    fitdist(data = x,distr = dist)
  },error = function(e){
    write(x = paste(Sys.time(),"Error in func_fit :",e$message,sep = " "),file = "error_log.txt",append = T,sep = "\n")
  })
}

# Creating a vector of repeated values which will result in an error
test<-rep(x = 1,times = 10)

func_desc(x = test)
# Results in an error and the message is written to the error log and not printed in the console

func_fit(x = test,dist = "beta")
# Results in an error and the message is both written to the error log and printed in the console

我想禁止func_fit() 打印此错误消息。

我已经尝试过以下替代方案:

  1. try()silent = TRUE。仍然会打印错误消息。
  2. conditionMessage() 给出相同的结果。
  3. withCallingHandlers() 已在一些帖子和线程中提出,但我不确定如何正确实施。
  4. invisible() 与函数一起使用仍会打印错误。

【问题讨论】:

  • 嘿,我认为问题是,您的错误消息来自 C 代码。 optim 调用源 .External2(C_optim, par, fn1, gr1, method, con, lower, upper) 中的 C_optim 文件调用 C_optim 函数。由于我不确定错误是否有这个起源,所以我没有写这个作为答案。这可以解释为什么你不能静音。 (见here)所以要么这对你有用,要么另一种方法是(不推荐)重写源代码。
  • 感谢您的回复。我尝试使用该线程中建议的解决方案,但 CRAN 上不再提供 multicore 包。 multicore 页面说 parallel 可以用作替代方案。我已经浏览了它的文档,但我不知道如何在我的情况下使用它。

标签: r error-handling try-catch error-suppression fitdistrplus


【解决方案1】:

这是因为fitdist(或者实际上,由fitdist 调用的mledist)已经在进行一些错误捕获。原始错误在optim 中并已被捕获,然后mledist 打印 到控制台的错误消息。因此,您看到的并不是真正的错误,甚至不是警告,而是包含捕获的错误消息内容的打印语句。

mledist 的作用是:

    if (inherits(opttryerror, "try-error")) {
        warnings("The function optim encountered an error and stopped.")
        if (getOption("show.error.messages")) 
            print(attr(opttryerror, "condition"))
        return(list(estimate = rep(NA, length(vstart)), convergence = 100, 
            loglik = NA, hessian = NA, optim.function = opt.fun, 
            fix.arg = fix.arg, optim.method = meth, fix.arg.fun = fix.arg.fun, 
            counts = c(NA, NA)))
    }

这不是很好的做法,正是因为它会导致您现在遇到的问题;它会阻止其他人系统地处理错误。

从该代码中可以看出,您可以通过将show.error.messages 选项设置为 FALSE 来关闭此功能:

options(show.error.messages = FALSE)

但您要小心这一点,因为在 R 会话的其余部分您不会看到任何错误消息。你绝对不想对别人的会话这样做。

另一种选择是使用sink("extra-error-messages.txt") 将所有打印发送到某个地方的控制台(甚至可能发送到您的error_log.txt,但我不确定这是否会导致写入多个内容时出现问题)。

【讨论】:

  • 感谢您的回复。它让我很好地了解了生成该错误消息的原因。我不想完全禁用错误消息打印,因为这些功能是我正在开发的包的一部分,这可能会导致安装包的任何人出现问题。我会尝试使用 sink 看看是否可行。
  • 使用接收器效果很好。由于我在多个变量上迭代这个函数,我最初得到一个错误,说“接收器堆栈已满”,但我可以解决这个问题。几个星期以来,我已经尝试了太多方法来解决这个问题。你无法想象此刻我不会再看到控制台上打印出那个丑陋的东西了。非常感谢您的帮助:)
猜你喜欢
  • 2020-03-23
  • 2015-01-26
  • 2013-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-20
  • 1970-01-01
相关资源
最近更新 更多