Re: [R] A question on generating Error message upon Timeout

2019-05-11 Thread Rui Barradas

Hello,

This solution works but when I've tried it, I realized it depends on the 
locale. In mine, pt_PT.UTF-8, the error message is in Portuguese and it 
failed. So I have two suggestions.


1) grep "Sys.sleep" in attr(n_Try, "condition")
2) not related but instead of length(grep(.)) > 0, logical grepl is simpler.

So the second 'if' in the function would become

if (grepl("Sys.sleep", attr(n_Try, "condition")) )

And now everything is Ok.

Hope this helps,

Rui Barradas

Às 11:57 de 11/05/19, Eric Berger escreveu:

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.christo...@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@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]]

__
R-help@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.



__
R-help@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.


Re: [R] A question on generating Error message upon Timeout

2019-05-11 Thread Eric Berger
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.christo...@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@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]]

__
R-help@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.


[R] A question on generating Error message upon Timeout

2019-05-09 Thread Christofer Bogaso
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@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.