Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
219 views
in Technique[技术] by (71.8m points)

traceback() for interactive and non-interactive R sessions

I observed a different between an interactive and non-interaction R session about traceback() which I do not understand. For the code below, it will produce an error, but in an interactive R session, I can see the traceback information, whereas if I save the code to test.R and call it via Rscript test.R or R -f test.R, I can no longer see the traceback:

f = function() {
  on.exit(traceback())
  1 + 'a'
}
f()

In an interactive R session:

> f = function() {
+   on.exit(traceback())
+   1 + 'a'
+ }
> f()
Error in 1 + "a" : non-numeric argument to binary operator
1: f()

Non-interactive execution:

$ Rscript test.R 
Error in 1 + "a" : non-numeric argument to binary operator
Calls: f
No traceback available 
Execution halted

I did not see an explanation in ?traceback, and I'm wondering if there is a way to enable traceback for non-interactive R sessions. Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

With default values of its arguments, traceback() will look for an object named .Traceback in the baseenv() for information on the call stack. It looks (from src/main/errors.c) like .Traceback is only created if, among other conditions, R_Interactive || haveHandler, suggesting that this object is not created during non-interactive sessions. If there is no object named .Traceback, you will get the message "No traceback available".

However, by passing a non-NULL value to the x argument of traceback(), one can obtain information about the call stack from a non-interactive session. With a non-zero integer value (indicating the number of calls to skip in the stack), c-level functions (R_GetTraceback) are called to investigate the call stack instead of looking in .Traceback.

So there are a couple ways to obtain traceback information in a non-interactive session:

f = function() {
  on.exit(traceback(1))
  1 + 'a'
}
f()

Or, setting options as Brandon Bertelsen suggested

options(error=function()traceback(2))

The different values passed to x in the two examples account for the different number of functions to skip

  1. In the on.exit example, traceback(1) skips the call to traceback().

  2. In the example setting options, there is an extra anonymous function that calls traceback() which should/could also be skipped.

In the example in the OP, there's not much more information gained by using traceback() compared to the automatic traceback provided in the case of an error in a non-interactive session. However, with functions that take (and are passed) arguments, using traceback() will be much more informative than the standard presentation of the call stack in the non-interactive session.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...