views:

30

answers:

2

If a warning or error occurs, I would like to print the name of the function generating the error.

tryCatch in R allows one to handle errors in a function call, perhaps it is part of the solution? For example, this could be in a context like:

handleErr <-function(e) {
    print("you had an error in function:")
    print( WHAT CAN I PUT HERE??! )
}

tryCatch(  myFunction(), error=handleErr )
+1  A: 

This should work

handleErr <- function(e) 
    {
    cat(paste("you had an error in function: ", e$call, "\n"))
    }


myfunct <- function()
    {
    stop()
    }

tryCatch(myfunct(), error=handleErr)
nico
A: 

Look at the traceback function, it shows the call stack for when the last error occured, so you can see what function had the error and also what function called that function, etc.

You can also set options(error= ) to a function to be called when an error occurs, see the help for options to see some functions that already work with this.

You can also set options(warn=2) to promote warnings into errors so that the above tools will work with warnings as well.

Greg Snow