How to save an error message in a string in R?

2

I would like to know if there is a function that saves the error message of an expression in a string.

For example I would like to get the result below:

erro <- pegaErro(runif("a"))
erro
Error in runif("a") : invalid arguments
In addition: Warning message:
In runif("a") : NAs introduced by coercion

That is, the object erro was a string with the error message.

I have two reasons why I want to do this:

  • My code is long and produces several error messages, I would like to save all of them to look at later
  • The error message for my function is too large and the console is cutting.
asked by anonymous 04.11.2014 / 13:06

2 answers

3

The geterrmessage() function returns the last error message. If you do not want anything very structured or efficient, it is possible to do a quick trick that will accumulate all the error messages in a vector.

Set as an option when you have an error:

options(error= expression(ifelse(exists("erros"), 
                                 erros <<- c(erros, geterrmessage()), 
                                 erros <<- geterrmessage())))

Then run your script. For example:

1 + "a"
runif("a")
funcaoquenaoexiste()

At the end you will have a vector named erros with all messages:

cat(erros)
 Error in 1 + "a" : non-numeric argument to binary operator
 Error in runif("a") : invalid arguments
 Error: could not find function "funcaoquenaoexiste"

Once you've done what you need, turn the errors back to normal with:

options(error= NULL)

Another "gambiarra" option is to use knitr. Write your script with markdown in knitr with the option error=TRUE and at the end you will have everything saved in an HTML document.

If you want something more structured and personalized, you'll have to start with try or tryCatch , as rcoster said. For example, the try function will save the error message if the expression does not execute correctly:

resultado <- try(runif("a"))
resultado
[1] "Error in runif(\"a\") : invalid arguments\n"
attr(,"class")
[1] "try-error"
attr(,"condition")
<simpleError in runif("a"): invalid arguments>

There are more elaborate things in answers to this SOen question.

Hadley has the evaluate package , which might also be useful for you wants to do:

library(evaluate)
erro <- evaluate('runif("a")')
str(erro)
List of 3
 $ :List of 1
  ..$ src: chr "runif(\"a\")"
  ..- attr(*, "class")= chr "source"
 $ :List of 2
  ..$ message: chr "NAs introduced by coercion"
  ..$ call   : language runif("a")
  ..- attr(*, "class")= chr [1:3] "simpleWarning" "warning" "condition"
 $ :List of 2
  ..$ message: chr "invalid arguments"
  ..$ call   : language runif("a")
  ..- attr(*, "class")= chr [1:3] "simpleError" "error" "condition"

Note that he took both the warning and error messages. The error message is the third element in the list:

erro[[3]]
<simpleError in runif("a"): invalid arguments>

erro[[3]]$message
[1] "invalid arguments"
    
04.11.2014 / 15:38
0

One way that can be useful is to encapsulate its function wrapped in the safely function of the purrr package.

A function encapsulated in safely always returns a list with two elements: the result and the error.

Example:

new_function <- function(x){
  x + 1
}
new_safe_function <- safely(new_function)

new_safe_function("a")

# $result
# NULL
# 
# $error
# <simpleError in x + 1: non-numeric argument to binary operator>
    
21.10.2016 / 14:06