0

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?

Joachim Schork
  • 2,025
  • 3
  • 25
  • 48
  • 1
    You can't and that's one reason why eval(parse()) programming is bad. – Roland Mar 11 '21 at 10:37
  • @Roland Thank you for the reply. That's bad news though. Is there maybe another function that is able to do that? – Joachim Schork Mar 11 '21 at 11:05
  • 1
    Do what? You should never have R code stored as R character strings. – Roland Mar 11 '21 at 11:55
  • @Roland Unfortunately, I don't have the possibility to import my code as real code, therefore I have to execute it as character string. – Joachim Schork Mar 11 '21 at 12:31
  • I don't understand "I don't have the possibility to import my code as real code". What do you believe what real code is? An R script is just a text file. – Roland Mar 11 '21 at 13:46
  • @Roland My input file contains text that can not be executed in R. I need to import this file as a character string first, and then I have to extract the code that I want to use in R from this character string. After this character string manipulation, my R code is formatted as a character string. Simplified example: I have a txt file with the following content "xxxxx my_variable <- 55 xxxxx". I can import this txt file as string, remove the xxxxx and eval/parse the code. I hope that makes sense. – Joachim Schork Mar 11 '21 at 14:52
  • @Roland As additional note: The same problem occurs when I try to run an R script using the source function. I have created an R script called error_test.R that contains nothing but the R code `5 5`. If I run this R script using `source("error_test.R")` the error output is also not the same as if I would run the script manually. – Joachim Schork Mar 11 '21 at 17:11
  • 1
    "My input file contains text that can not be executed in R. I need to import this file as a character string first, and then I have to extract the code that I want to use in R from this character string." The next step should be that you add your error handling to the clean code, then you can parse and evaluate that. (I would write the result to a text file and then just source that text file.) I still think your process is broken if you have R code mixed with some text. – Roland Mar 12 '21 at 06:41
  • @Roland I have followed your advice, and it works now for some errors (e.g. `list()[[0]]`). For the example of this thread (i.e. `5 5`) it still doesn't work though, and I have noticed that I am not even able to capture this error message properly with the error handlers suggested by [this thread](https://stackoverflow.com/questions/4948361/how-do-i-save-warnings-and-errors-as-output-from-a-function). So the follow-up question is: How can I capture the error output of a line of code such as `5 5`? Do you have an idea how to do that? Maybe this is a question for a new thread. – Joachim Schork Mar 12 '21 at 08:24
  • 1
    Error handling is not designed for code that can't be parsed. I still think you are trying to solve the wrong issue. – Roland Mar 12 '21 at 08:39
  • @Roland Unfortunately, I have to deal with the bad formatted text file as described above (due to non-R related issues). Maybe I'll open a new thread, because it seems like this follow-up question is different compared to the original question of this thread. Anyway, thank you very much for your help Roland! – Joachim Schork Mar 12 '21 at 08:42
  • Related to https://stackoverflow.com/questions/66603676/capture-error-messages-as-character-string-in-r/66660202#66660202 – barboulotte Apr 05 '21 at 16:30

0 Answers0