[R] A question on generating Error message upon Timeout

Eric Berger er|cjberger @end|ng |rom gm@||@com
Sat May 11 12:57:43 CEST 2019


Hi Christofer,
You have a number of misunderstandings.
The first thing you could have tried in order to figure out what was going
on was to remove the 'silent=TRUE' from the call to try().
This would then give you extra information, specifically that there was a
timeout. The exact message that gets printed on the call myFn(1) is
'Error in Sys.sleep(10) : reached elapsed time limit'
This confirms that your timeout condition is met, but that the error is
caught by the try() within myFn().
The class of the error is "try-error", the same as the class of the error
generated by the stop() statement, so the condition
if ( class(n_Try) == 'try-error' ) does not differentiate between the two
cases. You could modify the definition of myFn() with something like:

 myFn = function(n) {
    n_Try = try({
      if (n == 1) {Sys.sleep(10); Res = -123}
      if (n == 2) stop()
      if (n > 2) Res = 1:5
    })  #, silent = FALSE)
    if (class(n_Try) == "try-error") {
      if ( length(grep("reached elapsed time limit",n_Try)) > 0 )
         return("ERROR: Timeout")
      else
        return("ERROR : Good Error.")
    } else {
      return(Res)
    }
  }

HTH,
Eric


On Thu, May 9, 2019 at 10:37 PM Christofer Bogaso <
bogaso.christofer using gmail.com> wrote:

> Hi,
>
> I have created a function called myFn() which should be acting as below:
>
> 1. If function takes too long, then it will timeout and a specific message
> will be displayed
> 2. This function may generate error on its own, so if it evaluates before
> that specific time and fails then another specific message will be
> displayed
> 3. Within that specific time, if that function successfully run then the
> result will be displayed.
>
> To implement above strategy my function is as follows:
>
>
> *library(R.utils)*
> *myFn = function(n) {*
> * n_Try = try({*
> * if (n == 1) {Sys.sleep(10); Res = -123}*
> * if (n == 2) stop()*
> * if (n > 2) Res = 1:5 *
> * }, silent = TRUE)*
> * if (class(n_Try) == 'try-error') {*
> * return("ERROR : Good Error.")*
> * } else {*
> * return(Res)*
> * }*
> * }*
>
> Below is for case #1
>
> *aa = *
> *tryCatch({*
> *  res <- withTimeout({*
> *    myFn(1)*
> *  }, timeout = 2, onTimeout = 'error')*
> *}, error = function(ex) {*
> *  ("Timeout Error")*
> *}); aa*
>
> I was expecting to get the expression for *aa* as *"Timeout Error"*,
> however I am getting *"ERROR : Good Error."*. This is not going with my
> plan
>
> Also, I dont think this function getting timeout just after 2 sec, appears
> like it is evaluating for longer time
>
> However for *myFn(2)*, I am getting right message as "*"ERROR : Good
> Error."*"
>
> *aa = *
> *tryCatch({*
> *  res <- withTimeout({*
> *    myFn(2)*
> *  }, timeout = 2, onTimeout = 'error')*
> *}, error = function(ex) {*
> *  ("Timeout Error")*
> *}); aa*
>
> So clearly my strategy is failing for *myFn(1)*. Any pointer where I was
> wrong?
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list