Equivalent of "throw" in R

forkandwait picture forkandwait · Oct 22, 2009 · Viewed 67.8k times · Source

How does one "throw" an error in R? I have a function that takes a data frame and some column names and does stuff with them. If the columns don't exist, I want the function to stop and to stop all functions depending on it.

I have looked at recover and browse and traceback but, well, they seemed to be close but not what I am looking for.

Answer

Dirk Eddelbuettel picture Dirk Eddelbuettel · Oct 22, 2009

See help(tryCatch):

Conditions are signaled by 'signalCondition'. In addition, the
'stop' and 'warning' functions have been modified to also accept
condition arguments.

and later under 'See Also':

'stop' and 'warning' signal conditions, and 'try' is essentially a simplified version of 'tryCatch'.

so you probably want stop.