Error handling in a function

How would I trap an error in an argument passed to a function and simply quit the function? I have tried stop, but it sends me to debug the program. A user should enter a vector but they have a dataframe and rather than type in df$temp they forgot the $temp part.

my_func <- function(my_var){
  if(is.data.frame(my_var)) {stop("Please pass me a vector not a ",class(my_var))
  } else {
    print(my_var)
  }
}
my_func(gapminder)

I want the message printed in the console, but I want to suppress the debug feature.

Hi there,

stop() is the correct way to go. The debugging appears because you have debugging enabled.

In RStudio go to Debug >> On Error >> "Message-Only" or "Error Inspector".

Best,
Valentin

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.