How to check normal completion of R-scripts executed with Perl?

I wrote a shebang R script and would like to execute it with a Perl script. I am currently using system ($my_r_script_path, $r_script_arg1, $r_script_arg2, ...) and my question is: how can I check that the R script is terminating normally (no errors or warnings).

Suppose I have to make my R script return some true value at the end, only if everything is fine, and then catch that value in Perl, but I'm not sure how to do it.

Thanks!

+7
r perl return-value
source share
1 answer

You can set the return value in the quit() command, for example q(status=1) . The default is 0, see also "quit". How to catch this in Perl, it looks like you will catch any other return value in Perl. $? it stored in the special variable $? , If I remember it right. See Also examples in perldoc for system , there should be shown.

In the side, I just use the R-Perl interface. Here you can find information and examples: http://www.omegahat.org/RSPerl/

For completeness only:

At the beginning of your script, you can put something like:

 options( warn=2, # This will change all warnings into errors, # so warnings will also be handled like errors error= quote({ sink(file="error.txt"); # save the error message in a file dump.frames(); print(attr(last.dump,"error.message")); sink(); q("no",status=1,FALSE) # standard way for R to end after errors }) ) 

This will save the error message and exit the R session without saving with exit code 1 and without running .Last.

However, the R-Perl interface offers many more features that are worth checking out if you intend to do this more often.

+4
source share

All Articles