Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tryCatch does not catch an error if called though RScript

Tags:

r

try-catch

I'm facing a strange issue in R.

Consider the following code (a really simplified version of the real code but still having the problem) :

library(timeSeries)

tryCatch(
{
  specificWeekDay <- 2

  currTs <- timeSeries(c(1,2),c('2012-01-01','2012-01-02'),
                       format='%Y-%m-%d',units='A')
  # just 2 dates out of range
  start <- time(currTs)[2]+100*24*3600
  end <- time(currTs)[2]+110*24*3600

  # this line returns an empty timeSeries
  currTs <- window(currTs,start=start,end=end)

  message("Up to now, everything is OK")

  # this is the line with the uncatchable error
  currTs[!(as.POSIXlt(time(currTs))$wday %in% specificWeekDay),] <- NA

  message("I'm after the bugged line !")

},error=function(e){message(e)})

message("End")

When I run that code in RGui, I correctly get the following output:

Up to now, everything is OK
error in evaluating the argument 'i' in selecting a method for function '[<-': Error in as.POSIXlt.numeric(time(currTs)) : 'origin' must be supplied
End

Instead, when I run it through RScript (in windows) using the following line:

RScript.exe --vanilla "myscript.R"

I get this output:

Up to now, everything is OK
Execution interrupted

It seems like RScript crashes...

Any idea about the reason?
Is this a timeSeries package bug, or I'm doing something wrong ?
If the latter, what's the right way to be sure to catch all the errors ?

Thanks in advance.


EDIT :

Here's a smaller example reproducing the issue that doesn't use timeSeries package. To test it, just run it as described above:

library(methods)
# define a generic function
setGeneric("foo", 
           function(x, ...){standardGeneric("foo")})
# set a method for the generic function
setMethod("foo", signature("character"),
          function(x) {x})
tryCatch(
{
  foo("abc")
  foo(notExisting)
},error=function(e)print(e))

It seems something related to generic method dispatching; when an argument of a method causes an error, the dispatcher cannot find the signature of the method and conseguently raises an exception that tryCatch function seems unable to handle when run through RScript.
Strangely, it doesn't happen for example with print(notExisting); in that case the exception is correctly handled.

Any idea about the reason and how to catch this kind of errors ?

Note:
I'm using R-2.14.2 on Windows 7

like image 962
digEmAll Avatar asked Jul 09 '12 10:07

digEmAll


People also ask

How do I catch an error message in R?

In R Programming, there are basically two ways in which we can implement an error handling mechanism. Either we can directly call the functions like stop() or warning(), or we can use the error options such as “warn” or “warning. expression”.

Which errors Cannot be handled by catch block?

As is documented, try/catch blocks can't handle StackOverflowException and OutOfMemoryException.

How does tryCatch work in R?

The tryCatch() function in R evaluates an expression with the possibility to catch exceptions. The class of the exception thrown by a standard stop() call is try-error. The tryCatch() function allows the users to handle errors. With it, you can do things like: if(error), then(do this).

How do I turn off error messages in R?

To suppress the warning message, we can set warn=2 in the options function, or we can use suppressWarnings instead to mute the warning message. On the other hand, we can also use the stopifnot function to check whether the argument is valid or not.


1 Answers

The issue is in the way the internal C code implementing S4 method dispatch tries to catch and handle some errors and how the non-interactive case is treated in this approach. A work-around should be in place in R-devel and R-patched soon.

Work-around now committed to R-devel and R-patched.

like image 71
Luke Tierney Avatar answered Oct 05 '22 04:10

Luke Tierney