rainer
rainer

Reputation: 939

R and Error Handling

I'm trying to get a grip on exception handling in R, or exception handling as a whole.

I found this post which was useful in the first place. But now I don't know how I should handle a specific problem. Let's say we we have a list of Variables and I want the user to select (via svDialogs) one of these.

require(svDialogs)
var<-c("A","B","C","D")
var1<-dlgList(var,multiple=FALSE)$res

If the user selects nothing or hits cancel R returns an empty character string. I want to program to throw an error if that happens and quits the program. How can I achieve that? Thank you in advance.

Upvotes: 1

Views: 207

Answers (1)

Andrie
Andrie

Reputation: 179578

To throw an error, do some custom validation, then use stop():

For example:

if(length(var1)==0) stop("My error message)

Since your original code doesn't contain errors, just an empty string, tryCatch or any of the other error catching mechanisms doesn't help in your case.

Upvotes: 2

Related Questions