I want to capture the error outputs of some R codes, and usually I can do that as described in this thread.
However, some of my R codes are formatted as character strings. Usually, I could execute codes that are formatted as a character string using the eval and parse functions. My problem is that the parse function returns different error messages than R code that is not formatted as a character string.
The following example illustrates my problem. The R code below leads to the error message Error: unexpected numeric constant in "5 5"
:
5 5
# Error: unexpected numeric constant in "5 5"
But if I use the parse function to return my error message based on text input, the error message changes:
parse(text = "5 5")
# Error in parse(text = "5 5") : <text>:1:3: unexpected numeric constant
# 1: 5 5
# ^
So my question is: How can I return the same error message when the R code is formatted as a character string?
Is this possible with the parse function? Are there any other functions available that are able to do that?