CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

Post #1 of 11 (1508 views)
Calling the program "R" from within a perl script fails in Windows Can't Post

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

Post #2 of 11 (1505 views)
Re: [BarryHall] Calling the program "R" from within a perl script fails in Windows [In reply to] Can't Post

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

Post #3 of 11 (1502 views)
Re: [FishMonger] Calling the program "R" from within a perl script fails in Windows [In reply to] Can't Post

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

Post #4 of 11 (1500 views)
Re: [BarryHall] Calling the program "R" from within a perl script fails in Windows [In reply to] Can't Post

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

Post #5 of 11 (1490 views)
Re: [FishMonger] Calling the program "R" from within a perl script fails in Windows [In reply to] Can't Post

The path environment variable does contain the path to R.


FishMonger
Veteran / Moderator

Jun 18, 2010, 4:38 PM

Post #6 of 11 (1489 views)
Re: [BarryHall] Calling the program "R" from within a perl script fails in Windows [In reply to] Can't Post

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


BarryHall
Novice

Jun 18, 2010, 5:01 PM

Post #7 of 11 (1486 views)
Re: [FishMonger] Calling the program "R" from within a perl script fails in Windows [In reply to] Can't Post

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

Post #8 of 11 (1485 views)
Re: [BarryHall] Calling the program "R" from within a perl script fails in Windows [In reply to] Can't Post

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

Post #9 of 11 (1480 views)
Re: [FishMonger] Calling the program "R" from within a perl script fails in Windows [In reply to] Can't Post

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


shawnhcorey
Enthusiast


Jun 19, 2010, 4:38 AM

Post #10 of 11 (1470 views)
Re: [FishMonger] Calling the program "R" from within a perl script fails in Windows [In reply to] Can't Post


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

Post #11 of 11 (1467 views)
Re: [shawnhcorey] Calling the program "R" from within a perl script fails in Windows [In reply to] Can't Post


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

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives