我有一個大約1400行的R腳本。我最近開始收到以下錯誤消息。我可以通過突出顯示和使用ctrl-R來運行腳本,但是我無法在調試模式下運行腳本。有關如何找出錯誤的任何建議?R:在運行debugSource時fBody [[i]]出錯
> debugSource('~/working/R/h60_maintenance/do_mega_analysis.R')
Error in fBody[[i]] : subscript out of bounds
> options(error=recover)
> debugSource('~/working/R/h60_maintenance/do_mega_analysis.R')
Error in fBody[[i]] : subscript out of bounds
Enter a frame number, or 0 to exit
1: debugSource("~/working/R/h60_maintenance/do_mega_analysis.R")
2: (function (fileName, encoding, breaklines)
{
env <- new.env(parent = emptyenv())
env$fun <-
3: suppressWarnings(.rs.setFunctionBreakpoints("fun", env, lapply(steps, function(step) {
step$at
}
4: withCallingHandlers(expr, warning = function(w) invokeRestart("muffleWarning"))
5: .rs.setFunctionBreakpoints("fun", env, lapply(steps, function(step) {
step$at
}))
6: suppressMessages(trace(what = functionName, where = envir, at = lapply(strsplit(as.character(steps
7: withCallingHandlers(expr, message = function(c) invokeRestart("muffleMessage"))
8: trace(what = functionName, where = envir, at = lapply(strsplit(as.character(steps), ","), as.numer
9: eval.parent(call)
10: eval(expr, p)
11: eval(expr, envir, enclos)
12: methods::.TraceWithMethods(what = functionName, where = <environment>, at = lapply(strsplit(as.cha
13: new(traceClass, def = if (doEdit) def else original, tracer = tracer, exit = exit, at = at, print
14: initialize(value, ...)
15: initialize(value, ...)
16: .initTraceable(.Object, ...)
17: .makeTracedFunction(def, tracer, exit, at, print, doEdit)
該錯誤可能與以下幾行有關,因爲這些行與選項10相關聯:eval(expr,p)。
imds_rollup <- imds_detail_dt[,{
## if there's just one row in the group of ID's, return nothing
list(
count_every_fault = .N,
max_ci_value = max(CI.Value),
max_rotor_turn_time_air_sec = max(Rotor.Turn.Time...In.Air..s.),
max_rotor_turn_time_ground_sec = max(Rotor.Turn.Time...On.Ground..s.)
)}, by = c("BUNO","fileEventIndex")]
setkeyv(imds_rollup,c("BUNO","fileEventIndex"))
imds_rollup$max_ci_value <- NULL # max_ci_value has all NA
在RStudio中定位錯誤的一種方法是使用shift-ctrl-c註釋掉代碼塊以隔離錯誤的來源。我想知道是否可以修改debugSource的源代碼來打印檢測到錯誤的行號。 – user3969377 2014-10-29 13:14:57
我認爲RStudio通常也會在錯誤信息中顯示行號。如果你有問題找到導致錯誤的行號,你可以使用我的(新)包'tryCatchLog'(https://github.com/aryoda/tryCatchLog),它豐富了行號,甚至可以寫一個轉儲到磁盤,以便對錯誤進行事後分析。 – 2017-11-26 08:58:41