What is Error in value[[3L]](cond) in R?

本秂侑毒 提交于 2019-12-12 08:28:50

问题


I have a code that has an error because of not enough memory. Actually I do a linear model (lm) on a big data. The problem is not because it gives me the error, that I want to log, but because it contains value[[3L]](cond).

My error looks like this:

Error in value[[3L]](cond): While training model Error: cannot allocate vector of size 6.4 Gb

The code that logs it look like this (using logging lib):

tryCatch({
  # some code
  tryCatch({
    # some other code
  }, warning = function(war){
    logwarn(war, logger = "MyLogger")
  }, error = function(err){
    stop(paste("While training model", err, sep = " "))
  })
  some more code
}, error = function(err){
  logerror(err, logger = "MyLogger")
})

My problem is why is it saying Error in value[[3L]](cond):? Is it something wrong that I did and I do not know? Shouldn't it be just Error: <error message>?


回答1:


You are issuing stop() in your inner tryCatch, and internally, when an error condition is raised, tryCatch() calls the error handler you provided which is the third element in a list (internal to tryCatch). It calls that that handler passing condition cond via: value[[3L]](cond). Since your error handler calls stop, that's where the most recent error was called.

You can use traceback() (which implicitly calls print()) to view the call stack in the error handler like so:

tryCatch({
    stop('')
},error=function(err){
    traceback()
})

which yields:

5: print(where) at #4
4: value[[3L]](cond)
3: tryCatchOne(expr, names, parentenv, handlers[[1L]])
2: tryCatchList(expr, classes, parentenv, handlers)
1: tryCatch({
       stop()
   }, error = function(err) {
       print(where)
   })

If you want to retain the call-stack from the original error but have a more informative error message, just edit the error and re-raise it :

  tryCatch({
    # some other code
  }, warning = function(war){
    logwarn(war, logger = "MyLogger")
  }, error = function(err){
    # edit the error message
    err$message <- paste("While training model", err, sep = " ")
    # and re-raise
    stop(err)
  })


来源:https://stackoverflow.com/questions/31854304/what-is-error-in-value3lcond-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!