sys.call(1) will work now, but as my code evolves it tends
towards having more layers of function calls and so the count
given in sys.call(count) has to be altered.  This is a maintainance
problem.  Sometimes, as when using generic functions, you are
not even aware that the number of calls between two function
calls has changed.
 
On a more stylistic level, it seems that if the condition system
goes to the trouble of giving you the call as part of the input
to the error handler, why bother finding it in another way?

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 

 



________________________________

        From: Sebastien Bihorel [mailto:sebastien.biho...@cognigencorp.com] 
        Sent: Thursday, January 06, 2011 1:37 PM
        To: William Dunlap
        Cc: Henrique Dallazuanna; Sebastien Bihorel; R-help
        Subject: Re: [R] Stop and call objects
        
        
        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-boun...@r-project.org 
                        [mailto:r-help-boun...@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.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]]
                        
                        
                            


        [[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