Home: Perl Programming Help: Intermediate:
Calling the program "R" from within a perl script fails in Windows



BarryHall
Novice

Jun 18, 2010, 11:24 AM


Views: 2843
Calling the program "R" from within a perl script fails in Windows

I need to call the program "R" from within a Perl script. I do so using a system call like this:
@args = ('R --vanilla --quiet --file=Rblock');
system(@args) == 0 or die "system @args failed: $!";

The script works perfectly when run in Mac OSX or Linux. In Windows XP it fails with the message " 'R' is not recognized as an internal or external command, operable program or batch file".

To ensure that R was properly installed and that the environment variable "path" was correctly set, I executed the same command (R --vanilla --quiet --file=Rblock) from the command line and it worked perfectly; i.e. R ran with the expected behavior.

Similar system calls to other external programs, e.g.
@args = ('Notepad');
system(@args) == 0 or die "system @args failed: $!";
work perfectly.

Is there any obvious reason that this particular call works from the command line but not from within a perl script?

Thanks for any assistance


FishMonger
Veteran / Moderator

Jun 18, 2010, 12:30 PM


Views: 2840
Re: [BarryHall] Calling the program "R" from within a perl script fails in Windows

Try changing:

Code
@args = ('R --vanilla --quiet --file=Rblock');


To:

Code
@args = q(R --vanilla --quiet --file=Rblock);



BarryHall
Novice

Jun 18, 2010, 12:45 PM


Views: 2837
Re: [FishMonger] Calling the program "R" from within a perl script fails in Windows

Thanks for trying, but the change made no difference at all. The behavior remains identical
Barry Hall


FishMonger
Veteran / Moderator

Jun 18, 2010, 12:50 PM


Views: 2835
Re: [BarryHall] Calling the program "R" from within a perl script fails in Windows

Check your $ENV{PATH} setting to see if it includes the directory to "R". If it doesn't, then either adjust that var or include the full path in the system call.


BarryHall
Novice

Jun 18, 2010, 4:36 PM


Views: 2825
Re: [FishMonger] Calling the program "R" from within a perl script fails in Windows

The path environment variable does contain the path to R.


FishMonger
Veteran / Moderator

Jun 18, 2010, 4:38 PM


Views: 2824
Re: [BarryHall] Calling the program "R" from within a perl script fails in Windows

Then the answer is clear. You need to append it to that hash value.


BarryHall
Novice

Jun 18, 2010, 5:01 PM


Views: 2821
Re: [FishMonger] Calling the program "R" from within a perl script fails in Windows

Many thanks. I modified the code to read
my $R = "C:\\Program Files\\R\\R-2.11.1\\bin\\R";
my @args = ("$R","--vanilla", "--quiet", "--file=Rblock");

It now works just fine.

The only issue is that other users may install different versions of R or install it elsewhere. I think I will need to have my perl script search for the location of R.exe, use getcwd() to get the path to that folder, then use that to set the value of $R.


FishMonger
Veteran / Moderator

Jun 18, 2010, 5:15 PM


Views: 2820
Re: [BarryHall] Calling the program "R" from within a perl script fails in Windows

A better option would be to store the path in a config file and have the script read that config file.


BarryHall
Novice

Jun 18, 2010, 10:07 PM


Views: 2815
Re: [FishMonger] Calling the program "R" from within a perl script fails in Windows

Aha! Yes, I see how that would be a better option. Thank you.
BGH


shawnhcorey
Enthusiast


Jun 19, 2010, 4:38 AM


Views: 2805
Re: [FishMonger] Calling the program "R" from within a perl script fails in Windows


In Reply To
Try changing:

Code
@args = ('R --vanilla --quiet --file=Rblock');


To:

Code
@args = q(R --vanilla --quiet --file=Rblock);



Don't you mean?

Code
@args = qw(R --vanilla --quiet --file=Rblock);


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


FishMonger
Veteran / Moderator

Jun 19, 2010, 5:05 AM


Views: 2802
Re: [shawnhcorey] Calling the program "R" from within a perl script fails in Windows


In Reply To

In Reply To
Try changing:

Code
@args = ('R --vanilla --quiet --file=Rblock');


To:

Code
@args = q(R --vanilla --quiet --file=Rblock);



Don't you mean?

Code
@args = qw(R --vanilla --quiet --file=Rblock);



Yes, that is what I meant. Thanks for catching my error