Anne-Marie Ternes
2008-Jul-15 10:59 UTC
[R] tryCatch - return from function to main script
Dear helpers, I've got a main script, which calls 4 times a function on 4 different datasets respectively. This function runs "nls" and is located in another R script which is sourced into my main script. What I would like to have is this: If, e.g. in the 3rd call of the function, nls fails, because it can't converge, I would like it to return an error (value or message), and continue with the 4th call in my main script. I've tried "try", but it always completely stops execution. I've also played around with "tryCatch", but to be honest, the help page is quite cryptic to me. I'm sure "tryCatch" has a way of being told to "ok, stop this, and continue with the main script". As I'm quite in a hurry (need to finish this before leaving tomorrow), I'd be glad if you could give me a very practical example - I promise to look deeper into the details of exception handling in R when I'm back. Thanks a lot!! Anne-Marie
Can you provide an example of what you have tried. This should work: for(i in 1:4){ x <- try(yourFunction(...)) if (class(x) = "try-error") print('had a problem") } On Tue, Jul 15, 2008 at 6:59 AM, Anne-Marie Ternes <amternes at gmail.com> wrote:> Dear helpers, > > I've got a main script, which calls 4 times a function on 4 different > datasets respectively. This function runs "nls" and is located in > another R script which is sourced into my main script. > > What I would like to have is this: > > If, e.g. in the 3rd call of the function, nls fails, because it can't > converge, I would like it to return an error (value or message), and > continue with the 4th call in my main script. > > I've tried "try", but it always completely stops execution. I've also > played around with "tryCatch", but to be honest, the help page is > quite cryptic to me. I'm sure "tryCatch" has a way of being told to > "ok, stop this, and continue with the main script". > > As I'm quite in a hurry (need to finish this before leaving tomorrow), > I'd be glad if you could give me a very practical example - I promise > to look deeper into the details of exception handling in R when I'm > back. > > Thanks a lot!! > > Anne-Marie > > ______________________________________________ > R-help at r-project.org mailing list > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?