arunkumar1111
2011-Sep-27 04:20 UTC
[R] How to terminate R program if found any execution error
Hi
I want to terminate R process if there are any execution error.
a="a"
b=10
c=try(a/b)
if(class(c)[1]=="try-error")
{
stop("Wrong Input Value")
}
d=c*c
if c fails then it should terminate the process.
Please can anyone help
--
View this message in context:
http://r.789695.n4.nabble.com/How-to-terminate-R-program-if-found-any-execution-error-tp3846127p3846127.html
Sent from the R help mailing list archive at Nabble.com.
Duncan Murdoch
2011-Sep-27 05:22 UTC
[R] How to terminate R program if found any execution error
On 11-09-27 12:20 AM, arunkumar1111 wrote:> Hi > > I want to terminate R process if there are any execution error. > > a="a" > b=10 > c=try(a/b) > > if(class(c)[1]=="try-error") > { > stop("Wrong Input Value") > } > > d=c*c > > > > if c fails then it should terminate the process. > Please can anyone helpKeep the try(a/b), but replace the conditional with if (inherits(c, "try-error")) q("no") See ?q if you want to set an error status to be returned, or do want to save the workspace, etc. (And do use inherits() rather than comparing to a particular entry: your code will probably work in this example, but it's not the right way to test the class of something, and some day "try-error" might not be the first entry.) Duncan Murdoch