[R] need help with withRestarts ?

Chel Hee Lee chl948 at mail.usask.ca
Sun Dec 7 04:29:19 CET 2014


I am very happy to see the message replied by Martin Morgan.  He 
provides an example how to use the function 'withRestarts()'.  I 
personally like his approach; however, the function 'tryCatch()' 
evaluates the first argument 'expression'.  That's, this function can be 
placed on anywhere.  It seems to me that your primary goal is to make 
the function 'myfunc()' continue to work.  I hope that I correctly 
understand your question.  Please see the place where I put the function 
'tryCatch()' in your code.

 >
 > myfunc <- function(){
+   while(1){
+   x <- runif(1)
+   tryCatch( {
+ if ( x > 0.3 ) a <- x/2 else a <- x/"b"
+ print(a)
+ },
+     warning=function(w) print("warning"),
+     error=function(e) print("error"),
+     finally=cat("Error is printed if x < 0.3.  x=", x, "\nAnyway, move 
to next!\n") )
+   Sys.sleep(1)
+   }
+ }
 > myfunc()
[1] 0.2630333
Error is printed if x < 0.3.  x= 0.5260666
Anyway, move to next!
[1] "error"
Error is printed if x < 0.3.  x= 0.2929098
Anyway, move to next!
[1] 0.2123464
Error is printed if x < 0.3.  x= 0.4246928
Anyway, move to next!
[1] 0.2535644
Error is printed if x < 0.3.  x= 0.5071288
Anyway, move to next!
[1] "error"
Error is printed if x < 0.3.  x= 0.1941202
Anyway, move to next!
(continue)

I hope this helps, and thank you so much, Martin Morgan, for your good 
lesson.

Chel Hee Lee


On 12/06/2014 07:22 PM, Martin Morgan wrote:
> On 12/06/2014 02:53 PM, ce wrote:
>> Dear all,
>>
>> Let's say I have this script , below. tryCatch  indeed catches the
>> error but exists, I want function to continue and stay in the loop. I
>> found very  examples of withRestarts on internet to figure it out.
>> Could you help me how to do it ?
>>
>>
>> myfunc <- function()
>> {
>>    while(1)
>>    {
>>    x <- runif(1)
>>    if ( x > 0.3 ) a <-  x/2 else a <- x/"b"
>>    print(a)
>>    Sys.sleep(1)
>>    }
>> }
>
> Hi --
>
> Modify your function so that the code that you'd like to restart after
> is surrounded with withRestarts(), and with a handler that performs the
> action you'd like, so
>
> myfunc <- function()
> {
>      while(TRUE)
>      {
>          x <- runif(1)
>          withRestarts({
>              if ( x > 0.3 ) a <-  x/2 else a <- x/"b"
>              print(a)
>          }, restartLoop = function() {
>              message("restarting")
>              NULL
>          })
>          Sys.sleep(1)
>      }
> }
>
> Instead of using tryCatch(), which returns to the top level context to
> evaluate the handlers, use withCallingHandlers(), which retains the
> calling context. Write a handler that invokes the restart
>
> withCallingHandlers({
>      myfunc()
> }, error = function(e) {
>      message("error")
>      invokeRestart("restartLoop")
> })
>
> It's interesting that tryCatch is usually used with errors (because
> errors are hard to recover from), and withCallingHandlers are usually
> used with warnings (because warnings can usually be recovered from), but
> tryCatch() and withCallingHandlers() can be used with any condition.
>
> Martin
>
>>
>> tryCatch({ myfunc() },
>>          warning = function(w) { print("warning") },
>>          error = function(e) { print("error") },
>>          finally = {  print("end") }
>> )
>>
>> ______________________________________________
>> R-help at 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.
>>
>
>



More information about the R-help mailing list