Well, the goal is to include a reference to f2 in the error message 
returned by f2('char'); sys.call(1) appears to do the trick. You 
mentioned this function could be unreliable, could you please provide an 
example?

Sebastien

William Dunlap wrote:
>> -----Original Message-----
>> From: r-help-bounces@r-project.org 
>> [mailto:r-help-bounces@r-project.org] On Behalf Of Henrique 
>> Dallazuanna
>> Sent: Wednesday, January 05, 2011 9:26 AM
>> To: Sebastien Bihorel
>> Cc: R-help
>> Subject: Re: [R] Stop and call objects
>>
>> Try this:
>>
>> f <- function(x) 
>> tryCatch(sum(x),error=function(e)sprintf("Error in %s:
>> %s",  deparse(sys.call(1)), e$message))
>> f('a')
>>     
>
> The argument e to the error handler contains a call
> component so you don't have to rely on the unreliable
> sys.call(1) to get the offending call.  E.g.,
>  
>   > f2 <- function(x) {
>     tryCatch(sum(x),
>       error=function(e) {
>         sprintf("Error in %s: %s", deparse(e$call)[1], e$message)
>       }
>     )
>   }
>   > f2('char')
>   [1] "Error in sum(x): invalid 'type' (character) of argument"
>
> Bill Dunlap
> Spotfire, TIBCO Software
> wdunlap tibco.com 
>
>   
>> On Wed, Jan 5, 2011 at 12:23 PM, Sebastien Bihorel <
>> Sebastien.Bihorel@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]]
>>
>>
>>     

	[[alternative HTML version deleted]]

