5

How can i find out WHERE the error occurs?

i've got a double loop like this

companies <- # vector with all companies in my data.frame
dates <- # vector with all event dates in my data.frame

for(i in 1:length(companies)) { 
  events_k <- # some code that gives me events of one company at a time

  for{j in 1:nrow(events_k)) {
  # some code that gives me one event date at a time
  results <- # some code that calculates stuff for that event date

}

mylist[[i]] <- results # store the results in a list
}

In this code I got an error (it was something like error in max(i)...) The inner loop works perfectly. So by leaving out the outer loop and manually enter the company ID's until that error appeared, I found out for which company there was something wrong. My data.frame had letters in a vector with daily returns for that specific company.

For a next time: Is there a way in R to find out WHERE (or here FOR WHICH COMPANY) the error appears? It could save a lot of time!

cptn
  • 693
  • 2
  • 8
  • 28
  • 2
    an easy way is to print `i` & `j` in every loop run, so you know when it fails... – Christian Borck Apr 13 '14 at 14:27
  • You have a syntax error in the second for loop! You get this error: `Error: unexpected '{' in:`. I believe for this case it doesn't matter, but RStudio has a few debugging tools already included. – marbel Apr 13 '14 at 14:29
  • 2
    Type `traceback()`. See also `?debug`, `?trace`, `?browser`, `?recover` – GSee Apr 13 '14 at 14:31
  • you bros are not reading the question correctly. *This is not about a syntax error.* – rawr Apr 13 '14 at 14:56
  • 1
    Side note: Don't use `for` loops for this. This is a job for "split-apply-combine" functions. Have a look at packages plyr, dplyr, or data.table. – Roland Apr 13 '14 at 14:57
  • It is almost always a good idea to do some input validation when you get an error hinting at missing or invalid data. Check the datatypes, or test with things like `sum(is.na(dataframe))` to see if there are any NA values you didn't expect, etc. – Carl Witthoft Apr 13 '14 at 15:08
  • 1
    If you're in RStudio, the `Debug` drop-down menu options `On Error` and `Break in Code` are really handy. `On Error` gives an option to look at the `traceback`, which shows you what tripped the error and where it occurred. It's quite useful for handling errors. – Rich Scriven Apr 13 '14 at 15:15
  • Traceback spits out some number like "error max(i).. at #30". So first, I checked the 30th entry in my vector companies, but everything was fine! Does anyone know how to interpret this number traceback gives us? – cptn Apr 13 '14 at 15:39
  • If you want help interpretting some output, then provide code that [reproduces](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) it. – GSee Apr 13 '14 at 15:52

2 Answers2

5

What I like to use is:

options(error = recover)

You only need to run it once at the beginning of your session (or add it to your .Rprofile file)

After that, each time an error is thrown you will be shown the stack of function calls that led to the error. You can select any of these calls and it will be as if you had run that command in browser() mode: you will be able to look at the variables in the calling environment and walk through the code.

More info and examples at ?recover.

flodel
  • 87,577
  • 21
  • 185
  • 223
0

It is difficult to know without explicit code that we can run, but my guess is that changing your code to

 for(i in companies) { 
    for(j in dates) {

or alternatively

 for(i in 1:length(companies)) { 
    for(j in 1:length(dates)) {

may solve the issue. Note the ( in the second loop. If not, it might be a good idea to edit your example to have some code / data that produces the same error.

To figure out where it occurs, you can always add print(i) or something like that at a suitable place in the code.

coffeinjunky
  • 11,254
  • 39
  • 57
  • aww yeah! actually I used something like your second approach. Thank you. I edited the question! – cptn Apr 13 '14 at 15:34