當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


R debugger 事後調試

R語言 debugger 位於 utils 包(package)。

說明

轉儲評估環境(幀)並檢查轉儲幀的函數。

用法

dump.frames(dumpto = "last.dump", to.file = FALSE,
            include.GlobalEnv = FALSE)
debugger(dump = last.dump)

limitedLabels(value, maxwidth = getOption("width") - 5L)

參數

dumpto

一個字符串。要轉儲到的對象或文件的名稱。

to.file

合乎邏輯的。垃圾場是否應存放在R對象或文件?

include.GlobalEnv

邏輯指示除了 sys.frames() 之外是否還應包含 .GlobalEnv 環境的副本。在批處理作業中使用時特別有用。

dump

一個R轉儲對象創建者dump.frames.

value

要格式化的 listcall,例如用於用戶菜單。

maxwidth

limitedLabels() 的結果修剪到的可選長度;小於 40 或大於 1000 的值將被縮尾。

細節

要使用post-mortem調試,請設置選項error調用給dump.frames。默認情況下,這會轉儲到R對象last.dump在工作區中,但可以將其設置為轉儲到文件(通過調用生成的對象的轉儲)save)。轉儲的對象包含調用堆棧、活動環境和返回的最後一個錯誤消息geterrmessage.

當轉儲到文件時,dumpto給出轉儲對象的名稱,文件名包含‘.rda” 附加。

可以通過調用 debugger 檢查類 "dump.frames" 的轉儲對象。這將給出錯誤消息和可供重複選擇的環境列表。選擇環境後,將複製該環境並從副本中調用browser。請注意,並非原始幀中的所有信息都可用,例如尚未評估的 Promise 以及任何 ... 參數的內容。

如果安裝 dump.frames 作為錯誤處理程序,則即使在非交互式會話中也會繼續執行。請參閱示例了解如何轉儲然後退出。

limitedLabels(v) 接受 list 調用,其元素可能具有 srcref 屬性,並返回一個向量,該向量將這些屬性的格式化版本粘貼到元素的格式化版本上,所有最終 strtrim() med 到 maxwidth

看不見的NULL

注意

應用於閉包的 sys.parentenvironment 等函數在 debugger 內將無法正常工作。

如果在計算形式參數的默認值時發生錯誤,調試器將在嘗試檢查該環境時報告“遞歸默認參數表示”。

當然post-mortem調試將無法工作,如果R損壞太嚴重而無法生成和保存轉儲,例如,如果它已用完工作空間。

例子

## Not run: 
options(error = quote(dump.frames("testdump", TRUE)))

f <- function() {
    g <- function() stop("test dump.frames")
    g()
}
f()   # will generate a dump on file "testdump.rda"
options(error = NULL)

## possibly in another R session
load("testdump.rda")
debugger(testdump)
Available environments had calls:
1: f()
2: g()
3: stop("test dump.frames")

Enter an environment number, or 0 to exit
Selection: 1
Browsing in the environment with call:
f()
Called from: debugger.look(ind)
Browse[1]> ls()
[1] "g"
Browse[1]> g
function() stop("test dump.frames")
<environment: 759818>
Browse[1]>
Available environments had calls:
1: f()
2: g()
3: stop("test dump.frames")

Enter an environment number, or 0 to exit
Selection: 0

## A possible setting for non-interactive sessions
options(error = quote({dump.frames(to.file = TRUE); q(status = 1)}))

## End(Not run)

參考

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.

也可以看看

browser 了解 Browse 提示符下可用的操作。

options 用於設置error 選項; recover 是一個交互式調試器,其工作方式與 debugger 類似,但在錯誤發生後立即進行。

相關用法


注:本文由純淨天空篩選整理自R-devel大神的英文原創作品 Post-Mortem Debugging。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。