[R] How to terminate R program if found any execution error

Duncan Murdoch murdoch.duncan at gmail.com
Tue Sep 27 07:22:56 CEST 2011


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 help

Keep 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



More information about the R-help mailing list