Ho uno script R con circa 1400 righe. Recentemente ho iniziato a ricevere il seguente messaggio di errore. Sono in grado di eseguire lo script evidenziando e utilizzando Ctrl-R, ma non riesco a eseguire lo script in modalità di debug. Qualche suggerimento su come individuare l'errore?R: Errore in fBody [[i]] durante l'esecuzione di debugSource
> 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)
L'errore può essere correlato alle seguenti linee, poiché queste righe sono associate con l'opzione 10: eval (espressione, 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
Un modo per individuare l'errore in RStudio consiste nell'utilizzare shift-ctrl-c per commentare i blocchi di codice per isolare la fonte dell'errore. Mi chiedo se il codice sorgente per debugSource possa essere modificato per stampare il numero di riga in cui è stato rilevato l'errore. – user3969377
Penso che RStudio mostri anche i numeri di riga nei messaggi di errore normalmente. Se hai problemi a trovare il numero di riga che causa l'errore, potresti usare il mio (nuovo) pacchetto 'tryCatchLog' (https://github.com/aryoda/tryCatchLog) che arricchisce i numeri di riga e persino puoi scrivere una copia in disco per consentire l'analisi post-mortem dell'errore. –