R: Error in fBody[[i]] while running debugSource
Asked Answered
N

5

14

I have an R script with about 1400 lines. I recently started to get the following error message. I am able to run the script by highlighting and using ctrl-R, but I can't run the script in debug mode. Any suggestions on how to locate the error?

> 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)

The error may be related to the following lines, since these lines are associated with option 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
Nealon answered 27/10, 2014 at 10:56 Comment(2)
One way to locate the error in RStudio is to use shift-ctrl-c to comment out blocks of code to isolate the source of the error. I wonder if the source code for debugSource could be modified to print the line number where the error was detected.Nealon
I think RStudio does also show the line numbers in error messages normally. If you are having problems to find the line number that causes the error you could use my (new) package tryCatchLog (github.com/aryoda/tryCatchLog) which enriches the line numbers and even can write a dump to disk to allow post-mortem analysis of the error.Ceceliacecil
I
18

It's an R studio glitch, not a bug in your code. I resolved it by closing my R Studio project (without saving the project workspace) and then reopening it.

Ionopause answered 11/10, 2015 at 8:30 Comment(0)
M
48

I also had this problem. It was caused by invalid breakpoints: This happens when you set a breakpoint, and then modify the code moving the breakpoint to a comment or a blank line for example.

Just clear all breakpoints and re-Source.

Margarettemargarida answered 15/1, 2016 at 2:35 Comment(4)
This is a better answer than the accepted one IMO -- I'd rather reset the breakpoints than lose my session.Defeasance
This worked for me. I agree it should be the accepted answer.Arquebus
This also works for Shiny. The accepted answer did not clear the error but this did. Thanks very much.Strata
wow it is 2020 and this still pops up during debugsource(...) - kind of annoyingSymbiosis
I
18

It's an R studio glitch, not a bug in your code. I resolved it by closing my R Studio project (without saving the project workspace) and then reopening it.

Ionopause answered 11/10, 2015 at 8:30 Comment(0)
A
2

I've tested that one/some of the following might help (each bullet point is an individual fix):

  • Restart RStudio.
  • Check that a breakpoint has not become assigned to an empty line, where it seems that it's not possible to place a breakpoint. In this case, remove the offending breakpoints.
Ambroid answered 9/6, 2021 at 11:27 Comment(0)
H
0

Disclaimer: I am a complete newbie to R.

I was debugging a code that I received, in Rstudio. I got the same error. I cleaned the environment. I commented all lines but the first, and gradually uncommented larger portions of the code, until I was left with the original code. The error never popped.

This happened to me with two different scripts.

So, I do not know the reason for the error, but you may have a workaround for it.

Hartmann answered 3/10, 2015 at 10:4 Comment(0)
C
0

I was having this problem. The only message was:

'Error in fBody[[i]] : subscript out of bounds', no line number or any description of what the problem was.

I did have a breakpoint set and it seemed to be on a valid line, but when I cleared the breakpoint, the error went away.

Corson answered 23/7 at 23:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.