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

Allan Engelhardt allane at cybaea.com
Tue Sep 27 09:12:02 CEST 2011


The original poster may also be interested in options("error") to 
capture the 'any execution error' requirement.  From the examples in 
help("options"):

## Not run: ## on error, terminate the R session with error status 66
options(error = quote(q("no", status=66, runLast=FALSE)))
stop("test it")


Allan

On 27/09/2011 06:22, Duncan Murdoch wrote:
> 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
>
> ______________________________________________
> 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.



More information about the R-help mailing list