Re: [R] determining a parent function name

From: Sundar Dorai-Raj <sundar.dorai-raj_at_pdf.com>
Date: Thu, 31 May 2007 06:13:56 -0700

Hi, Vladimir,

In general, this won't work since traceback only contains the stack from the last uncaught error (see ?traceback). When traceback is called below it would be from the previous error, not the current one.

error <- function() {

   parent <- tail(capture.output(traceback()), n = 1)    parent <- sub("^.*:[ ]+", "", parent)    stop(parent)
}
foo <- function() error()
bar <- function() error()

 > foo()
Error in error() : No traceback available  > bar()
Error in error() : foo()

Thanks,

--sundar

Vladimir Eremeev said the following on 5/31/2007 4:57 AM:
> Does
> tail(capture.output(traceback()),n=1)
> do what you want?
>
> that is
>
> error <- function(...) {
> msg <- paste(..., sep = "")
> if(!length(msg)) msg <- ""
> if(require(tcltk, quiet = TRUE)) {
> tt <- tktoplevel()
> tkwm.title(tt, "Error")
> tkmsg <- tktext(tt, bg = "white")
>
> parent<-tail(capture.output(traceback()),n=1)
> parent<-gsub("[0-9]: ","",parent) # deleting 1: from the captured
> string
>
> tkinsert(tkmsg, "end", sprintf("Error in %s: %s", parent , msg))
> tkconfigure(tkmsg, state = "disabled", font = "Tahoma 12",
> width = 50, height = 3)
> tkpack(tkmsg, side = "bottom", fill = "y")
> }
> stop(msg)
> }
>
>
> Sundar Dorai-Raj wrote:

>> Hi, All,
>>
>> I'm writing a wrapper for stop that produces a popup window using tcltk. 
>> Something like:
>>
>> error <- function(...) {
>>    msg <- paste(..., sep = "")
>>    if(!length(msg)) msg <- ""
>>    if(require(tcltk, quiet = TRUE)) {
>>      tt <- tktoplevel()
>>      tkwm.title(tt, "Error")
>>      tkmsg <- tktext(tt, bg = "white")
>>      tkinsert(tkmsg, "end", sprintf("Error in %s: %s", "???", msg))
>>      tkconfigure(tkmsg, state = "disabled", font = "Tahoma 12",
>>                  width = 50, height = 3)
>>      tkpack(tkmsg, side = "bottom", fill = "y")
>>    }
>>    stop(msg)
>> }
>>
>> But, I would like to know from which function error() is called. For 
>> example, if I have
>>
>> foo <- function() stop()
>> bar <- function() error()
>>  > foo()
>> Error in foo() :
>>  > bar()
>> Error in error() :
>>
>> and in the tk window I get
>>
>> Error in ???:
>>
>> I need the output of bar (in the tk window only) to be
>>
>> Error in bar():
>>
>> then it's clear where error is called. I'm not worried about the output 
>> bar() produces on the console.
>>
>> Hope this makes sense.
>>
>> Thanks,
>>
>>
>

______________________________________________
R-help_at_stat.math.ethz.ch 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. Received on Thu 31 May 2007 - 13:35:44 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 31 May 2007 - 14:31:24 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.