Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>?

like image 804
sop Avatar asked Aug 06 '15 11:08

sop


1 Answers

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)
  })
like image 106
Jthorpe Avatar answered Oct 26 '22 07:10

Jthorpe