Continuous error management in R

After you hang up your typical R error messages, they are quite useful.

However, I often find my own R error messages are not as informative as I expected, so I usually write some kind of personalized error messages.

Now I wonder if there is any kind of systematic approach (or what the recommended best practice approach will be) regarding the assignment of unique error codes, as well as additional information for specific errors.

I suppose I would like to create some kind of error hash table solution where additional information can be stored and retrieved for each error. Would you use some kind of β€œlightweight” database solution (for example, SQLite) or would you solve it with a simple data.frame or list object that is stored somewhere ( .rdata ) and retrieved if necessary?

Homework

Honestly, I have not done this much research yet. Although there is at least something; -)

Reportr package

I just found out about the reportr package and will check it in more detail. Does anyone have experience with this package or similar?

Scheme of my own approach (partially pseudocode)

 myFoo <- function(x, ...) { tryCatch( x * 100, error=function(e) { record <- retrieveErrorRecord(e) # Does not exist yet # 'record' would be some sort of list or Ref Class Object if (!length(record)) { uid <- generateUid(e) # Does not exist yet msg <- paste( "expecting arg 'x' to be of class 'numeric' (was '", class(x), "')", sep="") insertErrorRecord( # Does not exist yet list( uid=uid, message=msg, original=e ) ) record <- retrieveErrorRecord(e) } msg <- c( "myFoo/error:\n", paste("* Code: ", record$uid, "\n", sep=""), paste("* Message: ", record$message, "\n", sep=""), paste("* Original: ", record$original, "\n", sep="") ) stop(msg) } ) } 

The way the message will look

 require("digest") x <- "abc" e <- simpleError("test error") record <- list( uid=digest(e), message=paste("expecting arg 'x' to be of class 'numeric' (was '", class(x), "')", sep=""), original=e ) msg <- c( "myFoo/error:\n", paste("* Code: ", record$uid, "\n", sep=""), paste("* Message: ", record$message, "\n", sep=""), paste("* Original: ", record$original, "\n", sep="") ) > stop(msg) Error: myFoo/error: * Code: e78e73054b93d2bf682df32845cd064d * Message: expecting arg 'x' to be of class 'numeric' (was 'character') * Original: Error: test error 
+4
source share
1 answer

IMHO, one way would be to "translate" the current R error messages into something more suitable for your needs. This can be done easily on the page (below?) Http://translation.r-project.org/pootle . And it will take a lot of work / time ...

But I think the standard R error / warning messages are pretty simple, although they are really technical, but hey, we are not programming or what? :)

But to give an answer / hint on how this could be done (with some regexp ), I applied something similar that could be found here in my eval.msgs (this is an assistant for a more reliable function called evals ).

This function works like evaluate , therefore it evaluates that the given R commands try to catch a possible informational / warning / error message, except for stdout and the returned raw object R. There I realized that syntax error messages are rather lame in one line, so I applied regexp above which leads to the examples below:

 > x <- 'foobar' > eval.msgs('x 100') $src [1] "x 100" $result NULL $output NULL $type [1] "error" $msg $msg$messages NULL $msg$warnings NULL $msg$errors [1] "Unexpected numeric constant at character 3 in line 1: ` x 100`" $stdout NULL > eval.msgs('x foo') $src [1] "x foo" $result NULL $output NULL $type [1] "error" $msg $msg$messages NULL $msg$warnings NULL $msg$errors [1] "Unexpected symbol at character 3 in line 1: ` x foo`" $stdout NULL > eval.msgs('x*100') $src [1] "x*100" $result NULL $output NULL $type [1] "error" $msg $msg$messages NULL $msg$warnings NULL $msg$errors [1] "non-numeric argument to binary operator" $stdout NULL 
+1
source

All Articles