Thank you very much Martin and Chel Hee. Indeed both approach works but Martin's approach covers whole function while Chel Hee's concentrates on problem line. Also another thanks to Martin for lesson on error handling which I desperately need. ce
-----Original Message----- From: "Chel Hee Lee" [chl...@mail.usask.ca] Date: 12/06/2014 10:29 PM To: "Martin Morgan" <mtmor...@fredhutch.org>, "ce" <zadi...@excite.com>, r-help@r-project.org Subject: Re: [R] need help with withRestarts ? 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@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.