Try this:

f <- function(x) tryCatch(sum(x),error=function(e)sprintf("Error in %s:
%s",  deparse(sys.call(1)), e$message))
f('a')





On Wed, Jan 5, 2011 at 12:23 PM, Sebastien Bihorel <
sebastien.biho...@cognigencorp.com> wrote:

> 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
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to