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,
--sundar
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,
>
>
--
View this message in context:
http://www.nabble.com/determining-a-parent-function-name-tf3843262.html#a10892459
Sent from the R help mailing list archive at Nabble.com.
Vladimir Eremeev wrote:> > Does > tail(capture.output(traceback()),n=1) > do what you want? > > that is >Hmmm... Seems, no... Having the earlier error() definition and bar<-function() error("asdasdf") ft<-function() bar()> ft()I get in the tcl/tk window: Error in bar(): asdasdf> bar()I get in the tcl/tk window: Error in ft(): asdasdf> I get in the tcl/tk window:Error in bar(): asdasdf Some kind of the stack flushing is needed. .Traceback<-NULL did not help -- View this message in context: http://www.nabble.com/determining-a-parent-function-name-tf3843262.html#a10892608 Sent from the R help mailing list archive at Nabble.com.
Hi, On Wednesday 30 May 2007 14:53:28 Sundar Dorai-Raj wrote:> 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) > }as.character(sys.call(-1)[[1]]) works for me. Best...
Sorry for replying to myself, but: On Thursday 31 May 2007 12:23:12 Ismail Onur Filiz wrote:> Hi, > > On Wednesday 30 May 2007 14:53:28 Sundar Dorai-Raj wrote: > > 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) > > } > > as.character(sys.call(-1)[[1]]) works for me.you can furthermore do: options(error=error) and remove the stop(msg) call in the last line of the function. Then your function will become the error handler. Best...> > Best... > > ______________________________________________ > 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.
Ismail Onur Filiz said the following on 5/31/2007 1:03 PM:> Sorry for replying to myself, but: > > On Thursday 31 May 2007 12:23:12 Ismail Onur Filiz wrote: >> Hi, >> >> On Wednesday 30 May 2007 14:53:28 Sundar Dorai-Raj wrote: >>> 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) >>> } >> as.character(sys.call(-1)[[1]]) works for me. > > you can furthermore do: > > options(error=error) > > and remove the stop(msg) call in the last line of the function. Then your > function will become the error handler. > > Best...Thanks, with the minor change to sys.call(-2) that does exactly what I want. thanks, --sundar