[R] Stop and call objects

Sebastien Bihorel Sebastien.Bihorel at cognigencorp.com
Wed Jan 5 15:23:40 CET 2011


Dear R-users,

Let's consider the following snippet:

f <- function(x)  tryCatch(sum(x),error=function(e) stop(e))
f('a')

As expected, the last call returns an error message: Error in sum(x) : 
invalid 'type' (character) of argument

My questions are the following:
1- can I easily ask the stop function to reference the "f" function in 
addition to "sum(x)" in the error message?
2- If not, I guess I would have to extract the call and message objects 
from e, coerce the call as a character object, build a custom string, 
and pass it to the stop function using call.=F. How can I coerce a call 
object to a character and maintain the "aspect" of the printed call 
(i.e. "sum(x)" instead of the character vector "sum" "x" returned by 
as.character(e$call))?

Thank you

Sebastien



More information about the R-help mailing list