什么参数传递给追溯中的函数? [英] What arguments were passed to the functions in the traceback?

查看:126
本文介绍了什么参数传递给追溯中的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在R中,如果由于出现错误而导致执行停止,我可以评估 traceback()以查看发生错误的函数,哪个函数是从该函数调用的,它会给出这样的东西:

In R, if execution stops because of an error, I can evaluate traceback() to see which function the error occurred in, which function was that function called from, etc. It'll give something like this:

8: ar.yw.default(x, aic = aic, order.max = order.max, na.action = na.action, 
       series = series, ...)
7: ar.yw(x, aic = aic, order.max = order.max, na.action = na.action, 
       series = series, ...)
6: ar(x[, i], aic = TRUE)
5: spectrum0.ar(x)
4: effectiveSize(x)

有没有办法找到传递给哪些参数这些功能?在这种情况下,我想知道哪些参数被传递给 effectiveSize(),即什么是 x

Is there a way to find what arguments were passed to these functions? In this case, I'd like to know what arguments were passed to effectiveSize(), i.e. what is x.

我自己的代码中不会出现错误,而是在一个包函数中。对于R来说,我有点迷失了。

The error does not occur in my own code, but in a package function. Being new to R, I'm a bit lost.

不知道如何正确地执行此操作,我试图找到包函数的定义并修改它,但是源文件应该是我只找到一个 .rdb 文件。我假设这是字节编译的。

Not knowing how to do this properly, I tried to find the package function's definition and modify it, but where the source file should be I only find an .rdb file. I assume this is something byte-compiled.

推荐答案

我建议设置 options(error = recover ),然后再次运行违规代码。这次遇到错误时,您将被投入到一个交互式调试环境中,您可以在其中提供可选择的框架进行调查。它会像 traceback()给你,除了您可以键入 7 >在调用堆栈中输入调用 7 的评估环境。输入框架后,输入 ls()将会提供参数列表。

I'd suggest setting options(error=recover) and then running the offending code again. This time, when an error is encountered, you'll be thrown into an interactive debugging environment in which you are offered a choice of frames to investigate. It will look much like what traceback() gives you, except that you can type 7 to enter the evaluation environment of call 7 on the call stack. Typing ls() once you've entered a frame will give you the list of its arguments.

(基于?traceback )可能是显示这个的最好方式:

An example (based on that in ?traceback) is probably the best way to show this:

foo <- function(x) { print(1); bar(2) }
bar <- function(x) { x + a.variable.which.does.not.exist }

## First with traceback()
foo(2) # gives a strange error
# [1] 1
# Error in bar(2) : object 'a.variable.which.does.not.exist' not found
traceback()
# 2: bar(2) at #1
# 1: foo(2)

## Then with options(error=recover)
options(error=recover)
foo(2) 
# [1] 1
# Error in bar(2) : object 'a.variable.which.does.not.exist' not found
# 
# Enter a frame number, or 0 to exit   
# 
# 1: foo(2)
# 2: #1: bar(2)

Selection: 1
# Called from: top level 
Browse[1]> ls()
# [1] "x"
Browse[1]> x
# [1] 2
Browse[1]>    ## Just press return here to go back to the numbered list of envts.
# 
# Enter a frame number, or 0 to exit   
# 
# 1: foo(2)
# 2: #1: bar(2)

R有许多有用的调试工具,其中大部分在答案中讨论这个SO问题几年前。

R has many helpful debugging tools, most of which are discussed in the answers to this SO question from a few years back.

这篇关于什么参数传递给追溯中的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆