2014-10-27 45 views
5

我有一個大約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 
+1

在RStudio中定位錯誤的一種方法是使用shift-ctrl-c註釋掉代碼塊以隔離錯誤的來源。我想知道是否可以修改debugSource的源代碼來打印檢測到錯誤的行號。 – user3969377 2014-10-29 13:14:57

+0

我認爲RStudio通常也會在錯誤信息中顯示行號。如果你有問題找到導致錯誤的行號,你可以使用我的(新)包'tryCatchLog'(https://github.com/aryoda/tryCatchLog),它豐富了行號,甚至可以寫一個轉儲到磁盤,以便對錯誤進行事後分析。 – 2017-11-26 08:58:41

回答

8

這是一個R工作室故障,而不是您的代碼中的錯誤。我通過關閉R Studio項目(不保存項目工作區)然後重新打開它解決了這個問題。

0

免責聲明:我是一個完整的新手到R.

我調試,我收到,在Rstudio代碼。我得到了同樣的錯誤。 我清理了環境。我評論了所有行,但第一個,並逐漸取消註釋的代碼較大的部分,直到我留下原始代碼。錯誤從未彈出。

這發生在我身上,有兩種不同的腳本。

所以,我不知道錯誤的原因,但您可能有一個解決方法。

22

我也有這個問題。它是由無效的斷點造成的:這種情況發生在設置斷點時,然後修改將斷點移動到註釋或空行的代碼。

只需清除所有斷點並重新來源。

+0

這適用於桌面和服務器 – tjim 2016-01-21 04:37:42

+2

這比接受的IMO更好 - 我寧願重置斷點而不會丟失會話。 – knowah 2016-03-21 13:28:32

+1

這對我有用。我同意它應該是被接受的答案。 – JerryKur 2016-09-04 18:23:31