Re: [R] determining a parent function name
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 __ R-help@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.
Re: [R] determining a parent function name
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@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. __ R-help@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.
Re: [R] determining a parent function name
Thanks! That's the answer I was looking for. --sundar Ismail Onur Filiz said the following on 5/31/2007 12:23 PM: > 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... > > __ > R-help@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. __ R-help@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.
Re: [R] determining a parent function name
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... __ R-help@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.
Re: [R] determining a parent function name
Thanks for the input. I don't think this will help either since it still requires you know the error occurred in foo. I settled on passing the call to error: error <- function(..., call) {} foo <- function() error("some error", call = match.call()) Thanks, --sundar Martin Morgan said the following on 5/31/2007 7:51 AM: > Hi sundar -- > > maybe > >> myerr <- function(err) err$call >> foo <- function() stop() >> tryCatch({ foo() }, error=myerr) > foo() > > suggests a way to catch errors without having to change existing code > or re-invent stop? > > Martin > > > Sundar Dorai-Raj <[EMAIL PROTECTED]> writes: > >> Hi, Vladimir, >> >> Sorry, didn't see this reply. .Traceback <- NULL doesn't work because of >> the warning in ?traceback. >> >> Warning: >> >> It is undocumented where '.Traceback' is stored nor that it is >> visible, and this is subject to change. Prior to R 2.4.0 it was >> stored in the workspace, but no longer. >> >> Thanks, >> >> --sundar >> >> Vladimir Eremeev said the following on 5/31/2007 5:10 AM: >>> >>> 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 >> __ >> R-help@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. > __ R-help@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.
Re: [R] determining a parent function name
Hi sundar -- maybe > myerr <- function(err) err$call > foo <- function() stop() > tryCatch({ foo() }, error=myerr) foo() suggests a way to catch errors without having to change existing code or re-invent stop? Martin Sundar Dorai-Raj <[EMAIL PROTECTED]> writes: > Hi, Vladimir, > > Sorry, didn't see this reply. .Traceback <- NULL doesn't work because of > the warning in ?traceback. > > Warning: > > It is undocumented where '.Traceback' is stored nor that it is > visible, and this is subject to change. Prior to R 2.4.0 it was > stored in the workspace, but no longer. > > Thanks, > > --sundar > > Vladimir Eremeev said the following on 5/31/2007 5:10 AM: >> >> >> 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 > > __ > R-help@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. -- Martin Morgan Bioconductor / Computational Biology http://bioconductor.org __ R-help@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.
Re: [R] determining a parent function name
Hi, Vladimir, Sorry, didn't see this reply. .Traceback <- NULL doesn't work because of the warning in ?traceback. Warning: It is undocumented where '.Traceback' is stored nor that it is visible, and this is subject to change. Prior to R 2.4.0 it was stored in the workspace, but no longer. Thanks, --sundar Vladimir Eremeev said the following on 5/31/2007 5:10 AM: > > > 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 __ R-help@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.
Re: [R] determining a parent function name
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@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.
Re: [R] determining a parent function name
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. __ R-help@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.
Re: [R] determining a parent function name
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. __ R-help@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.
[R] determining a parent function name
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 __ R-help@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.