Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to verify normal termination of R scripts executed from Perl?

I have written a shebang R script and would like to execute it from a Perl script. I currently use system ($my_r_script_path, $r_script_arg1, $r_script_arg2, ...) and my question is how can I verify the R script terminates normally (no errors or warnings).

guess I should make my R script return some true value at the end, only if everything is OK, then catch this value in Perl, but I'm not sure how to do that.

Thanks!

like image 976
David B Avatar asked Sep 08 '10 06:09

David B


1 Answers

You can set the return value in the command quit(), eg q(status=1). Default is 0, see also ?quit. How to catch that one in Perl, is like catching any other returning value in Perl. It is saved in a special variable $? if I remember right. See also the examples in the perldoc for system, it should be illustrated there.

On a sidenote, I'd just use the R-Perl interface. You can find info and examples here : http://www.omegahat.org/RSPerl/

Just for completeness :

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 break out of the R session without saving, with exit code 1 and without running the .Last.

Still, the R-Perl interface offers a lot more possibilities that are worth checking out if you're going to do this more often.

like image 68
Joris Meys Avatar answered Nov 10 '22 20:11

Joris Meys