Stack trace?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Stack trace?

rkevinburton

Currently I have a for loop executing functions and at the end I get a
message like:

There were 50 or more warnings (use warnings() to see the first 50)

If I do what it says and type warnings(), I get 50 messages like:

2: In !is.na(x) & !is.na(rowSums(xreg)) :
   longer object length is not a multiple of shorter object length

I am not sure what function these errors are originating from. I don't
think it is from any of the 'R' script that I wrote. I would like to see
which function is being called when this error is thrown and which
called that . . . and so on.

I have the same problem with error messages. An error is thrown but I
don't have a call stack to help trace down the problem. Is there some
function or technique that I could use to help get a call stack?

Thank you.

Kevin

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Stack trace?

Thomas Lumley-2
On Thu, Nov 10, 2011 at 10:35 AM,  <[hidden email]> wrote:

>
> Currently I have a for loop executing functions and at the end I get a
> message like:
>
> There were 50 or more warnings (use warnings() to see the first 50)
>
> If I do what it says and type warnings(), I get 50 messages like:
>
> 2: In !is.na(x) & !is.na(rowSums(xreg)) :
>   longer object length is not a multiple of shorter object length
>
> I am not sure what function these errors are originating from. I don't
> think it is from any of the 'R' script that I wrote. I would like to see
> which function is being called when this error is thrown and which
> called that . . . and so on.
>
> I have the same problem with error messages. An error is thrown but I
> don't have a call stack to help trace down the problem. Is there some
> function or technique that I could use to help get a call stack?

traceback() gets you a stack trace at the last error

options(warn=2) makes warnings into errors

options(error=recover) starts the post-mortem debugger at any error,
allowing you to inspect the stack interactively.

  -thomas

--
Thomas Lumley
Professor of Biostatistics
University of Auckland

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Stack trace?

William Dunlap
> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]] On Behalf Of Thomas Lumley
> Sent: Wednesday, November 09, 2011 1:53 PM
> To: [hidden email]
> Cc: r-help
> Subject: Re: [R] Stack trace?
>
> On Thu, Nov 10, 2011 at 10:35 AM,  <[hidden email]> wrote:
> >
> > Currently I have a for loop executing functions and at the end I get a
> > message like:
> >
> > There were 50 or more warnings (use warnings() to see the first 50)
> >
> > If I do what it says and type warnings(), I get 50 messages like:
> >
> > 2: In !is.na(x) & !is.na(rowSums(xreg)) :
> >   longer object length is not a multiple of shorter object length
> >
> > I am not sure what function these errors are originating from. I don't
> > think it is from any of the 'R' script that I wrote. I would like to see
> > which function is being called when this error is thrown and which
> > called that . . . and so on.
> >
> > I have the same problem with error messages. An error is thrown but I
> > don't have a call stack to help trace down the problem. Is there some
> > function or technique that I could use to help get a call stack?
>
> traceback() gets you a stack trace at the last error
>
> options(warn=2) makes warnings into errors
>
> options(error=recover) starts the post-mortem debugger at any error,
> allowing you to inspect the stack interactively.

And
  options(warning.expression=quote(recover()))
will start that same debugger at each warning.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com

 

>   -thomas
>
> --
> Thomas Lumley
> Professor of Biostatistics
> University of Auckland
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Stack trace?

Bert Gunter
To Bill's suggestion for a stack trace on warnings:
Question: What would this do in situations where one might get, e.g. 100
warnings?

-- Bert

On Wed, Nov 9, 2011 at 3:08 PM, William Dunlap <[hidden email]> wrote:

> > -----Original Message-----
> > From: [hidden email] [mailto:[hidden email]]
> On Behalf Of Thomas Lumley
> > Sent: Wednesday, November 09, 2011 1:53 PM
> > To: [hidden email]
> > Cc: r-help
> > Subject: Re: [R] Stack trace?
> >
> > On Thu, Nov 10, 2011 at 10:35 AM,  <[hidden email]> wrote:
> > >
> > > Currently I have a for loop executing functions and at the end I get a
> > > message like:
> > >
> > > There were 50 or more warnings (use warnings() to see the first 50)
> > >
> > > If I do what it says and type warnings(), I get 50 messages like:
> > >
> > > 2: In !is.na(x) & !is.na(rowSums(xreg)) :
> > >   longer object length is not a multiple of shorter object length
> > >
> > > I am not sure what function these errors are originating from. I don't
> > > think it is from any of the 'R' script that I wrote. I would like to
> see
> > > which function is being called when this error is thrown and which
> > > called that . . . and so on.
> > >
> > > I have the same problem with error messages. An error is thrown but I
> > > don't have a call stack to help trace down the problem. Is there some
> > > function or technique that I could use to help get a call stack?
> >
> > traceback() gets you a stack trace at the last error
> >
> > options(warn=2) makes warnings into errors
> >
> > options(error=recover) starts the post-mortem debugger at any error,
> > allowing you to inspect the stack interactively.
>
> And
>  options(warning.expression=quote(recover()))
> will start that same debugger at each warning.
>
> Bill Dunlap
> Spotfire, TIBCO Software
> wdunlap tibco.com
>
>
> >   -thomas
> >
> > --
> > Thomas Lumley
> > Professor of Biostatistics
> > University of Auckland
> >
> > ______________________________________________
> > [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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.
>



--

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Stack trace?

William Dunlap
It will call recover() for each warning.  When you exit recover()
the code continues on.  This is handy if you expect the first warning
or two but are curious about the third.   I'd expect that you could
reset options(warning.expression=NULL) when in recover() so
that recover() would not be called at the remaining warnings.

> options(warning.expression=quote(recover()))
> f <- function() {
+     warning("Hmmm")
+     retval <- c(1,2) + c(5,6,7)
+     warning("Really?")
+     retval
+ }
> f()

Enter a frame number, or 0 to exit

1: f()
2: #2: warning("Hmmm")
3: #2: .signalSimpleWarning("Hmmm", quote(f()))
4: #2: withRestarts({
5: #2: withOneRestart(expr, restarts[[1]])
6: #2: doWithOneRestart(return(expr), restart)

Selection: 0

Enter a frame number, or 0 to exit

1: f()
2: #3: .signalSimpleWarning("longer object length is not a multiple of shorter object length", quote(c(1, 2) + c(5, 6, 7)))
3: #3: withRestarts({
4: #3: withOneRestart(expr, restarts[[1]])
5: #3: doWithOneRestart(return(expr), restart)

Selection: 1
Called from: .signalSimpleWarning("longer object length is not a multiple of shorter object length",
    quote(c(1, 2) + c(5, 6, 7)))
Browse[1]> options(warning.expression=NULL)
Browse[1]> # hit control-D
Browse[1]>

Enter a frame number, or 0 to exit

1: f()
2: #3: .signalSimpleWarning("longer object length is not a multiple of shorter object length", quote(c(1, 2) + c(5, 6, 7)))
3: #3: withRestarts({
4: #3: withOneRestart(expr, restarts[[1]])
5: #3: doWithOneRestart(return(expr), restart)

Selection: 0
[1] 6 8 8
Warning message:
In f() : Really?

I think you could also use tryCatch(warning=function(e)...) to only call recover() at certain types
of warnings, perhaps by examining the text in conditionMessage(e).

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
From: Bert Gunter [mailto:[hidden email]]
Sent: Wednesday, November 09, 2011 3:40 PM
To: William Dunlap
Cc: r-help
Subject: Re: [R] Stack trace?

To Bill's suggestion for a stack trace on warnings:
Question: What would this do in situations where one might get, e.g. 100 warnings?

-- Bert
On Wed, Nov 9, 2011 at 3:08 PM, William Dunlap <[hidden email]<mailto:[hidden email]>> wrote:

> -----Original Message-----
> From: [hidden email]<mailto:[hidden email]> [mailto:[hidden email]<mailto:[hidden email]>] On Behalf Of Thomas Lumley
> Sent: Wednesday, November 09, 2011 1:53 PM
> To: [hidden email]<mailto:[hidden email]>
> Cc: r-help
> Subject: Re: [R] Stack trace?
>
> On Thu, Nov 10, 2011 at 10:35 AM,  <[hidden email]<mailto:[hidden email]>> wrote:
> >
> > Currently I have a for loop executing functions and at the end I get a
> > message like:
> >
> > There were 50 or more warnings (use warnings() to see the first 50)
> >
> > If I do what it says and type warnings(), I get 50 messages like:
> >
> > 2: In !is.na<http://is.na>(x) & !is.na<http://is.na>(rowSums(xreg)) :
> >   longer object length is not a multiple of shorter object length
> >
> > I am not sure what function these errors are originating from. I don't
> > think it is from any of the 'R' script that I wrote. I would like to see
> > which function is being called when this error is thrown and which
> > called that . . . and so on.
> >
> > I have the same problem with error messages. An error is thrown but I
> > don't have a call stack to help trace down the problem. Is there some
> > function or technique that I could use to help get a call stack?
>
> traceback() gets you a stack trace at the last error
>
> options(warn=2) makes warnings into errors
>
> options(error=recover) starts the post-mortem debugger at any error,
> allowing you to inspect the stack interactively.

And
 options(warning.expression=quote(recover()))
will start that same debugger at each warning.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com<http://tibco.com>


>   -thomas
>
> --
> Thomas Lumley
> Professor of Biostatistics
> University of Auckland
>
> ______________________________________________
> [hidden email]<mailto:[hidden email]> 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.

______________________________________________
[hidden email]<mailto:[hidden email]> 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.



--

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm



        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Stack trace?

William Dunlap
Bert,

At the end of my previous message I mentioned tryCatch().
I should have said withCallingHandlers().  E.g.,

> f <- function() {
+     warning("Hmmm")
+     retval <- c(1,2) + c(5,6,7) # warns about incompatible lengths in +
+     warning("Really?")
+     retval
+ }
> options(warning.expression=NULL) # just in case it had been set
> withCallingHandlers(f(), warning=function(e){
+     cat("encountered warning: ", conditionMessage(e), "\n")
+     if (grepl("multiple of shorter", conditionMessage(e))) { recover() }
+     invokeRestart("muffleWarning")
+ })
encountered warning:  Hmmm
encountered warning:  longer object length is not a multiple of shorter object length

Enter a frame number, or 0 to exit  

1: withCallingHandlers(f(), warning = function(e) {
2: f()
3: #3: .signalSimpleWarning("longer object length is not a multiple of shorter object length", quote(c(1, 2) + c(5, 6, 7)))
4: #3: withRestarts({
5: #3: withOneRestart(expr, restarts[[1]])
6: #3: doWithOneRestart(return(expr), restart)
7: #3: function (e)

Selection: 0
encountered warning:  Really?
[1] 6 8 8

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com

> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]] On Behalf Of William Dunlap
> Sent: Wednesday, November 09, 2011 3:51 PM
> To: Bert Gunter
> Cc: r-help
> Subject: Re: [R] Stack trace?
>
> It will call recover() for each warning.  When you exit recover()
> the code continues on.  This is handy if you expect the first warning
> or two but are curious about the third.   I'd expect that you could
> reset options(warning.expression=NULL) when in recover() so
> that recover() would not be called at the remaining warnings.
>
> > options(warning.expression=quote(recover()))
> > f <- function() {
> +     warning("Hmmm")
> +     retval <- c(1,2) + c(5,6,7)
> +     warning("Really?")
> +     retval
> + }
> > f()
>
> Enter a frame number, or 0 to exit
>
> 1: f()
> 2: #2: warning("Hmmm")
> 3: #2: .signalSimpleWarning("Hmmm", quote(f()))
> 4: #2: withRestarts({
> 5: #2: withOneRestart(expr, restarts[[1]])
> 6: #2: doWithOneRestart(return(expr), restart)
>
> Selection: 0
>
> Enter a frame number, or 0 to exit
>
> 1: f()
> 2: #3: .signalSimpleWarning("longer object length is not a multiple of shorter object length",
> quote(c(1, 2) + c(5, 6, 7)))
> 3: #3: withRestarts({
> 4: #3: withOneRestart(expr, restarts[[1]])
> 5: #3: doWithOneRestart(return(expr), restart)
>
> Selection: 1
> Called from: .signalSimpleWarning("longer object length is not a multiple of shorter object length",
>     quote(c(1, 2) + c(5, 6, 7)))
> Browse[1]> options(warning.expression=NULL)
> Browse[1]> # hit control-D
> Browse[1]>
>
> Enter a frame number, or 0 to exit
>
> 1: f()
> 2: #3: .signalSimpleWarning("longer object length is not a multiple of shorter object length",
> quote(c(1, 2) + c(5, 6, 7)))
> 3: #3: withRestarts({
> 4: #3: withOneRestart(expr, restarts[[1]])
> 5: #3: doWithOneRestart(return(expr), restart)
>
> Selection: 0
> [1] 6 8 8
> Warning message:
> In f() : Really?
>
> I think you could also use tryCatch(warning=function(e)...) to only call recover() at certain types
> of warnings, perhaps by examining the text in conditionMessage(e).
>
> Bill Dunlap
> Spotfire, TIBCO Software
> wdunlap tibco.com
> From: Bert Gunter [mailto:[hidden email]]
> Sent: Wednesday, November 09, 2011 3:40 PM
> To: William Dunlap
> Cc: r-help
> Subject: Re: [R] Stack trace?
>
> To Bill's suggestion for a stack trace on warnings:
> Question: What would this do in situations where one might get, e.g. 100 warnings?
>
> -- Bert
> On Wed, Nov 9, 2011 at 3:08 PM, William Dunlap <[hidden email]<mailto:[hidden email]>> wrote:
> > -----Original Message-----
> > From: [hidden email]<mailto:[hidden email]> [mailto:r-help-bounces@r-
> project.org<mailto:[hidden email]>] On Behalf Of Thomas Lumley
> > Sent: Wednesday, November 09, 2011 1:53 PM
> > To: [hidden email]<mailto:[hidden email]>
> > Cc: r-help
> > Subject: Re: [R] Stack trace?
> >
> > On Thu, Nov 10, 2011 at 10:35 AM,  <[hidden email]<mailto:[hidden email]>>
> wrote:
> > >
> > > Currently I have a for loop executing functions and at the end I get a
> > > message like:
> > >
> > > There were 50 or more warnings (use warnings() to see the first 50)
> > >
> > > If I do what it says and type warnings(), I get 50 messages like:
> > >
> > > 2: In !is.na<http://is.na>(x) & !is.na<http://is.na>(rowSums(xreg)) :
> > >   longer object length is not a multiple of shorter object length
> > >
> > > I am not sure what function these errors are originating from. I don't
> > > think it is from any of the 'R' script that I wrote. I would like to see
> > > which function is being called when this error is thrown and which
> > > called that . . . and so on.
> > >
> > > I have the same problem with error messages. An error is thrown but I
> > > don't have a call stack to help trace down the problem. Is there some
> > > function or technique that I could use to help get a call stack?
> >
> > traceback() gets you a stack trace at the last error
> >
> > options(warn=2) makes warnings into errors
> >
> > options(error=recover) starts the post-mortem debugger at any error,
> > allowing you to inspect the stack interactively.
>
> And
>  options(warning.expression=quote(recover()))
> will start that same debugger at each warning.
>
> Bill Dunlap
> Spotfire, TIBCO Software
> wdunlap tibco.com<http://tibco.com>
>
>
> >   -thomas
> >
> > --
> > Thomas Lumley
> > Professor of Biostatistics
> > University of Auckland
> >
> > ______________________________________________
> > [hidden email]<mailto:[hidden email]> 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.
>
> ______________________________________________
> [hidden email]<mailto:[hidden email]> 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.
>
>
>
> --
>
> Bert Gunter
> Genentech Nonclinical Biostatistics
>
> Internal Contact Info:
> Phone: 467-7374
> Website:
> http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Stack trace?

Michael Weylandt
Very nifty tricks re: getting recover on warnings. Thanks,

Michael

On Wed, Nov 9, 2011 at 7:11 PM, William Dunlap <[hidden email]> wrote:

> Bert,
>
> At the end of my previous message I mentioned tryCatch().
> I should have said withCallingHandlers().  E.g.,
>
>> f <- function() {
> +     warning("Hmmm")
> +     retval <- c(1,2) + c(5,6,7) # warns about incompatible lengths in +
> +     warning("Really?")
> +     retval
> + }
>> options(warning.expression=NULL) # just in case it had been set
>> withCallingHandlers(f(), warning=function(e){
> +     cat("encountered warning: ", conditionMessage(e), "\n")
> +     if (grepl("multiple of shorter", conditionMessage(e))) { recover() }
> +     invokeRestart("muffleWarning")
> + })
> encountered warning:  Hmmm
> encountered warning:  longer object length is not a multiple of shorter object length
>
> Enter a frame number, or 0 to exit
>
> 1: withCallingHandlers(f(), warning = function(e) {
> 2: f()
> 3: #3: .signalSimpleWarning("longer object length is not a multiple of shorter object length", quote(c(1, 2) + c(5, 6, 7)))
> 4: #3: withRestarts({
> 5: #3: withOneRestart(expr, restarts[[1]])
> 6: #3: doWithOneRestart(return(expr), restart)
> 7: #3: function (e)
>
> Selection: 0
> encountered warning:  Really?
> [1] 6 8 8
>
> Bill Dunlap
> Spotfire, TIBCO Software
> wdunlap tibco.com
>
>> -----Original Message-----
>> From: [hidden email] [mailto:[hidden email]] On Behalf Of William Dunlap
>> Sent: Wednesday, November 09, 2011 3:51 PM
>> To: Bert Gunter
>> Cc: r-help
>> Subject: Re: [R] Stack trace?
>>
>> It will call recover() for each warning.  When you exit recover()
>> the code continues on.  This is handy if you expect the first warning
>> or two but are curious about the third.   I'd expect that you could
>> reset options(warning.expression=NULL) when in recover() so
>> that recover() would not be called at the remaining warnings.
>>
>> > options(warning.expression=quote(recover()))
>> > f <- function() {
>> +     warning("Hmmm")
>> +     retval <- c(1,2) + c(5,6,7)
>> +     warning("Really?")
>> +     retval
>> + }
>> > f()
>>
>> Enter a frame number, or 0 to exit
>>
>> 1: f()
>> 2: #2: warning("Hmmm")
>> 3: #2: .signalSimpleWarning("Hmmm", quote(f()))
>> 4: #2: withRestarts({
>> 5: #2: withOneRestart(expr, restarts[[1]])
>> 6: #2: doWithOneRestart(return(expr), restart)
>>
>> Selection: 0
>>
>> Enter a frame number, or 0 to exit
>>
>> 1: f()
>> 2: #3: .signalSimpleWarning("longer object length is not a multiple of shorter object length",
>> quote(c(1, 2) + c(5, 6, 7)))
>> 3: #3: withRestarts({
>> 4: #3: withOneRestart(expr, restarts[[1]])
>> 5: #3: doWithOneRestart(return(expr), restart)
>>
>> Selection: 1
>> Called from: .signalSimpleWarning("longer object length is not a multiple of shorter object length",
>>     quote(c(1, 2) + c(5, 6, 7)))
>> Browse[1]> options(warning.expression=NULL)
>> Browse[1]> # hit control-D
>> Browse[1]>
>>
>> Enter a frame number, or 0 to exit
>>
>> 1: f()
>> 2: #3: .signalSimpleWarning("longer object length is not a multiple of shorter object length",
>> quote(c(1, 2) + c(5, 6, 7)))
>> 3: #3: withRestarts({
>> 4: #3: withOneRestart(expr, restarts[[1]])
>> 5: #3: doWithOneRestart(return(expr), restart)
>>
>> Selection: 0
>> [1] 6 8 8
>> Warning message:
>> In f() : Really?
>>
>> I think you could also use tryCatch(warning=function(e)...) to only call recover() at certain types
>> of warnings, perhaps by examining the text in conditionMessage(e).
>>
>> Bill Dunlap
>> Spotfire, TIBCO Software
>> wdunlap tibco.com
>> From: Bert Gunter [mailto:[hidden email]]
>> Sent: Wednesday, November 09, 2011 3:40 PM
>> To: William Dunlap
>> Cc: r-help
>> Subject: Re: [R] Stack trace?
>>
>> To Bill's suggestion for a stack trace on warnings:
>> Question: What would this do in situations where one might get, e.g. 100 warnings?
>>
>> -- Bert
>> On Wed, Nov 9, 2011 at 3:08 PM, William Dunlap <[hidden email]<mailto:[hidden email]>> wrote:
>> > -----Original Message-----
>> > From: [hidden email]<mailto:[hidden email]> [mailto:r-help-bounces@r-
>> project.org<mailto:[hidden email]>] On Behalf Of Thomas Lumley
>> > Sent: Wednesday, November 09, 2011 1:53 PM
>> > To: [hidden email]<mailto:[hidden email]>
>> > Cc: r-help
>> > Subject: Re: [R] Stack trace?
>> >
>> > On Thu, Nov 10, 2011 at 10:35 AM,  <[hidden email]<mailto:[hidden email]>>
>> wrote:
>> > >
>> > > Currently I have a for loop executing functions and at the end I get a
>> > > message like:
>> > >
>> > > There were 50 or more warnings (use warnings() to see the first 50)
>> > >
>> > > If I do what it says and type warnings(), I get 50 messages like:
>> > >
>> > > 2: In !is.na<http://is.na>(x) & !is.na<http://is.na>(rowSums(xreg)) :
>> > >   longer object length is not a multiple of shorter object length
>> > >
>> > > I am not sure what function these errors are originating from. I don't
>> > > think it is from any of the 'R' script that I wrote. I would like to see
>> > > which function is being called when this error is thrown and which
>> > > called that . . . and so on.
>> > >
>> > > I have the same problem with error messages. An error is thrown but I
>> > > don't have a call stack to help trace down the problem. Is there some
>> > > function or technique that I could use to help get a call stack?
>> >
>> > traceback() gets you a stack trace at the last error
>> >
>> > options(warn=2) makes warnings into errors
>> >
>> > options(error=recover) starts the post-mortem debugger at any error,
>> > allowing you to inspect the stack interactively.
>>
>> And
>>  options(warning.expression=quote(recover()))
>> will start that same debugger at each warning.
>>
>> Bill Dunlap
>> Spotfire, TIBCO Software
>> wdunlap tibco.com<http://tibco.com>
>>
>>
>> >   -thomas
>> >
>> > --
>> > Thomas Lumley
>> > Professor of Biostatistics
>> > University of Auckland
>> >
>> > ______________________________________________
>> > [hidden email]<mailto:[hidden email]> 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.
>>
>> ______________________________________________
>> [hidden email]<mailto:[hidden email]> 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.
>>
>>
>>
>> --
>>
>> Bert Gunter
>> Genentech Nonclinical Biostatistics
>>
>> Internal Contact Info:
>> Phone: 467-7374
>> Website:
>> http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
>>
>>
>>
>>       [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Stack trace?

rkevinburton
In reply to this post by William Dunlap
Will traceback() work in the error routine specified in tryCatch?

error <- function(e)
{
    traceback()
}

tryCatch(..., error=error)

-----Original Message-----
From: William Dunlap [mailto:[hidden email]]
Sent: Wednesday, November 09, 2011 5:09 PM
To: Thomas Lumley; [hidden email]
Cc: r-help
Subject: RE: [R] Stack trace?

> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of Thomas Lumley
> Sent: Wednesday, November 09, 2011 1:53 PM
> To: [hidden email]
> Cc: r-help
> Subject: Re: [R] Stack trace?
>
> On Thu, Nov 10, 2011 at 10:35 AM,  <[hidden email]> wrote:
> >
> > Currently I have a for loop executing functions and at the end I get
> > a message like:
> >
> > There were 50 or more warnings (use warnings() to see the first 50)
> >
> > If I do what it says and type warnings(), I get 50 messages like:
> >
> > 2: In !is.na(x) & !is.na(rowSums(xreg)) :
> >   longer object length is not a multiple of shorter object length
> >
> > I am not sure what function these errors are originating from. I
> > don't think it is from any of the 'R' script that I wrote. I would
> > like to see which function is being called when this error is thrown
> > and which called that . . . and so on.
> >
> > I have the same problem with error messages. An error is thrown but
> > I don't have a call stack to help trace down the problem. Is there
> > some function or technique that I could use to help get a call stack?
>
> traceback() gets you a stack trace at the last error
>
> options(warn=2) makes warnings into errors
>
> options(error=recover) starts the post-mortem debugger at any error,
> allowing you to inspect the stack interactively.

And
  options(warning.expression=quote(recover()))
will start that same debugger at each warning.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com

 

>   -thomas
>
> --
> Thomas Lumley
> Professor of Biostatistics
> University of Auckland
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Stack trace?

William Dunlap
You asked
> Will traceback() work in the error routine specified in tryCatch?

No, for two reasons.

First, R's traceback(), unlike S+'s in certain circumstances,
does not create the stack trace.  It just prints it, based on
the value of some structure that the standard error processor creates.
You need to call sys.calls() (and perhaps sys.frames()) to get the current
stack information.

Second, tryCatch's error handler only gets called after the
error has been thrown, after the stack has been torn down.
The argument to the handler, e, contains only the error message
and the call that caused the error (use conditionMessage(e) and
conditionCall(e) to extract those things).  At that point, sys.calls()
doesn't say anything about what was happening when the error was about
to be thrown.  Perhaps there is some way to attach the whole call
stack or all the call frames to the condition passed to the tryCatch's
condition handler, but I don't know it.

You can use withCallingHandlers instead, as with my example with warnings
yesterday.  E.g.,
  > f <- function(x) {
  +      f.inner <- function() x + "string"
  +      f.inner()
  +   }
  > withCallingHandlers(f(10), error=function(e)cat(conditionMessage(e), sapply(sys.calls(),function(sc)deparse(sc)[1]), sep="\n   "))
  non-numeric argument to binary operator
     withCallingHandlers(f(10), error = function(e) cat(conditionMessage(e),
     f(10)
     f.inner()
     .handleSimpleError(function (e)
     h(simpleError(msg, call))
  Error in x + "string" : non-numeric argument to binary operator
I think it is easier to use options("error") for this sort of thing.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com

> -----Original Message-----
> From: Kevin Burton [mailto:[hidden email]]
> Sent: Thursday, November 10, 2011 7:11 AM
> To: William Dunlap; 'Thomas Lumley'
> Cc: 'r-help'
> Subject: RE: [R] Stack trace?
>
> Will traceback() work in the error routine specified in tryCatch?
>
> error <- function(e)
> {
>     traceback()
> }
>
> tryCatch(..., error=error)
>
> -----Original Message-----
> From: William Dunlap [mailto:[hidden email]]
> Sent: Wednesday, November 09, 2011 5:09 PM
> To: Thomas Lumley; [hidden email]
> Cc: r-help
> Subject: RE: [R] Stack trace?
>
> > -----Original Message-----
> > From: [hidden email]
> > [mailto:[hidden email]] On Behalf Of Thomas Lumley
> > Sent: Wednesday, November 09, 2011 1:53 PM
> > To: [hidden email]
> > Cc: r-help
> > Subject: Re: [R] Stack trace?
> >
> > On Thu, Nov 10, 2011 at 10:35 AM,  <[hidden email]> wrote:
> > >
> > > Currently I have a for loop executing functions and at the end I get
> > > a message like:
> > >
> > > There were 50 or more warnings (use warnings() to see the first 50)
> > >
> > > If I do what it says and type warnings(), I get 50 messages like:
> > >
> > > 2: In !is.na(x) & !is.na(rowSums(xreg)) :
> > >   longer object length is not a multiple of shorter object length
> > >
> > > I am not sure what function these errors are originating from. I
> > > don't think it is from any of the 'R' script that I wrote. I would
> > > like to see which function is being called when this error is thrown
> > > and which called that . . . and so on.
> > >
> > > I have the same problem with error messages. An error is thrown but
> > > I don't have a call stack to help trace down the problem. Is there
> > > some function or technique that I could use to help get a call stack?
> >
> > traceback() gets you a stack trace at the last error
> >
> > options(warn=2) makes warnings into errors
> >
> > options(error=recover) starts the post-mortem debugger at any error,
> > allowing you to inspect the stack interactively.
>
> And
>   options(warning.expression=quote(recover()))
> will start that same debugger at each warning.
>
> Bill Dunlap
> Spotfire, TIBCO Software
> wdunlap tibco.com
>
>
> >   -thomas
> >
> > --
> > Thomas Lumley
> > Professor of Biostatistics
> > University of Auckland
> >
> > ______________________________________________
> > [hidden email] 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.

______________________________________________
[hidden email] 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.