Reputation: 13113
I use code that counts syntax errors and reports the number of syntax errors after the program has run. The error-counting code was provided on stackoverflow in response to my earlier question: R: is there a command for the end of a file that states whether any errors occurred?
Sometimes I forget to comment out a print message when analyzing large data sets and R cannot print all of the data and all of the code.
[ reached getOption("max.print") -- omitted 498 rows ]
When that happens and the error-counting code reports an error I cannot simply scroll back up to see what the error was. Is there a way to locate the error after the R code has run? I tried using traceback()
but it did not help. I have never used traceback()
and maybe I did not use it correctly. Other potential solutions I have found online seem to require inserting code before running the R file.
I could just rerun the R code with the print command commented out, but in this case the code takes several hours to run. Maybe I could rerun the code quickly with a smaller data set to find the error, but that assumes the size of the data set is not somehow causing the error.
Here is an example program containing an error. If n
is changed to a large number, maybe 10000000, this code seems like it will create the same scenario or a scenario similar to what I am describing above. Thank you for any advice.
I usually run my code by saving it in a *.r file, then copying the contents of that file and pasting it into the default R GUI placed on my Dell PC 64-bit Windows 7 Professional desktop during installation of the R application.
# the four lines below are for counting syntax errors
.error.count <- 0
old.error.fun <- getOption("error")
new.error.fun <- quote(.error.count <- .error.count + 1)
options(error = new.error.fun, width=2400)
##########################################################
n <- 10
a <- rnorm(n,10,4)
b <- rnorm(n,50,8)
c <- EXP(b)
d <- a + b
df <- data.frame(a,b,d)
df
##########################################################
# the three lines below count the number of errors in the code above
cat("ERROR COUNT:", .error.count, "\n")
options(error = old.error.fun)
rm(.error.count, old.error.fun, new.error.fun)
##########################################################
traceback()
# No traceback available
Upvotes: 3
Views: 196
Reputation: 44614
Here's a slightly different approach that uses the local
function to make an error logger that contains the log.
error.logger <- local({
error.log <- list() # initial empty log
function () {
# each time called, add to the log
error.log <<- c(error.log, geterrmessage())
}
})
options(error=error.logger, show.error.locations=TRUE)
This is basically the same as Andrie's approach, but avoids the global variable .error.log
. You can access the log with get('error.log', environment(error.logger))
. show.error.locations=TRUE
will include source line numbers in your error message.
This works whether you're in interactive or batch mode.
Upvotes: 1
Reputation: 179428
Here is one option that only works in interactive mode, AFAICT.
Modify your preamble to write the error message to an error log variable:
.error.log <- NULL
old.error.fun <- getOption("error")
new.error.fun <- quote({
.error.count <- .error.count + 1
.error.log <- c(.error.log, geterrmessage())
})
Then run your code and cat()
the value of the error log:
cat("ERROR COUNT:", .error.count, "\n")
cat("ERROR LOG:", .error.log, collapse="\n")
The results:
> cat("ERROR COUNT:", .error.count, "\n")
ERROR COUNT: 1
> cat("ERROR LOG:", .error.log, collapse="\n")
ERROR LOG: Error: could not find function "EXP"
Upvotes: 1