Skip to content
This repository has been archived by the owner on Nov 21, 2020. It is now read-only.

can not start #7

Open
AhmedAly90 opened this issue Mar 8, 2015 · 20 comments
Open

can not start #7

AhmedAly90 opened this issue Mar 8, 2015 · 20 comments

Comments

@AhmedAly90
Copy link

i use php-r but it give me an error
Fatal error: Uncaught exception 'Kachkaev\PHPR\Exception\RProcessException' with message '''' is not recognized as an internal or external command, ' in C:\xampp\htdocs\R\src\Kachkaev\PHPR\Process\CommandLineRProcess.php:31 Stack trace: #0 C:\xampp\htdocs\R\src\Kachkaev\PHPR\Process\AbstractRProcess.php(37): Kachkaev\PHPR\Process\CommandLineRProcess->doStart() #1 C:\xampp\htdocs\R\poorman.php(15): Kachkaev\PHPR\Process\AbstractRProcess->start() #2 {main} thrown in C:\xampp\htdocs\R\src\Kachkaev\PHPR\Process\CommandLineRProcess.php on line 31

and my code is to simple
use Kachkaev\PHPR\RCore;
use Kachkaev\PHPR\Engine\CommandLineREngine;
echo "

";
echo "Number values to generate: ";
echo "Number values to generate: ";
echo "";
echo "";
require('src\Kachkaev\PHPR\RCore.php');
require('src\Kachkaev\PHPR\Engine\CommandLineREngine.php');
require_once('src\Kachkaev\PHPR\Engine\AbstractREngine.php');
$r = new RCore(new CommandLineREngine('/usr/bin/R'));
$rProcess = $r->createInteractiveProcess();
+$rProcess->start();
$rProcess->write('x=10');
$rProcess->write('N=10');
$rProcess->write('png(filename="temp.png", width=500, height=500)');
$rProcess->write(' hist(x, col="lightblue")');
$rProcess->write('plot(x,N)');
$rProcess->write('dev.off()');
echo("

");

kachkaev added a commit that referenced this issue Mar 8, 2015
replace /usr/bin/R with /path/to/R to avoid confusion like in #7
@kachkaev
Copy link
Owner

kachkaev commented Mar 8, 2015

Hi @AhmedAly90,

It looks like PHP can't locate R. Default path "'/usr/bin/R" usually works for linux/unix, but is not likely to be the same on Windows. You should change this according to where R is installed in your system (php-r is not a replacement of R, it's just a bridge to it). Whatever path to R interpreter you define, the command should work in the console outside PHP. When you enter "/path/to/R" in the console, you should be able to start typing native R commands and instantly get the result (this is similar to what happens after you enter "mysql" in the console).

Could you please let me know what path worked? I'll update it in the readme file.

@AhmedAly90
Copy link
Author

i give the path on my windows but it doesnot work
$r = new RCore(new CommandLineREngine('C:/Program Files/R/R-3.1.2/bin/x64/R.exe'));
$rProcess = $r->createInteractiveProcess();
+$rProcess->start();
$rProcess->write('x=10');
$rProcess->write('N=10');
$rProcess->write('png(filename="temp.png", width=500, height=500)');
$rProcess->write(' hist(x, col="lightblue")');
$rProcess->write('plot(x,N)');
$rProcess->write('dev.off()');
echo("");

error given
Fatal error: Uncaught exception 'Kachkaev\PHPR\Exception\RProcessException' with message '''cd' is not recognized as an internal or external command, ' in C:\xampp\htdocs\R\src\Kachkaev\PHPR\Process\CommandLineRProcess.php:31 Stack trace: #0 C:\xampp\htdocs\R\src\Kachkaev\PHPR\Process\AbstractRProcess.php(37): Kachkaev\PHPR\Process\CommandLineRProcess->doStart() #1 C:\xampp\htdocs\R\poorman.php(15): Kachkaev\PHPR\Process\AbstractRProcess->start() #2 {main} thrown in C:\xampp\htdocs\R\src\Kachkaev\PHPR\Process\CommandLineRProcess.php on line 31
in the cmd i give it the path and it works correctly

@kachkaev
Copy link
Owner

kachkaev commented Mar 8, 2015

I just did a couple of experiments on a virtual windows machine (which does not have R) and concluded that the issue actually might be related to a problem in library code. Could you please try opening php-r/src/Kachkaev/PHPR/Process/CommandLineRProcess.php and replace line 28?

sprintf("'%s' --silent --vanilla", $this->rCommand)
↓
sprintf("\"%s\" --silent --vanilla", $this->rCommand)

If this solves the issue, I'll update the code.

@jackpolifka
Copy link
Contributor

Hi @kachkaev,

Very cool library. Thank you for sharing it.

I used your library yesterday and got to a point where I tried your solution on Windows. It solves the problem of not being able to find R on a Windows filesystem. After that though, my command hangs on line 37 for the following:

$errorOutput = fgets($this->pipes[2]);

I don't know if you want to create a new issue for that issue or not. If I find a solution, I'll make a pull request.

@kachkaev
Copy link
Owner

Hi @jackdp,

Unfortunately I currently don’t have enough resources to investigate PHP-R problems on Windows, so if you could run your own little investigation, that would be really great! Looking forward for your thoughts or pull requests.

@jackpolifka
Copy link
Contributor

On line 36 of php-r/src/Kachkaev/PHPR/Process/CommandLineRProcess.php, stream_set_blocking is returning false. From what I found on http://php.net/manual/en/function.stream-set-blocking.php and other sources, that function doesn't work on Windows.

When I had $pipes[2] errors write to a error.log file, I was able to successfully use R . What are your thoughts on writing files to a error log vs reading them from console?

Pending your opinion, I'll make the necessary changes and create a pull request.

@jackpolifka
Copy link
Contributor

I tried writing errors to a log instead of reading them using the pipe. It isn't the correct solution in my opinion.

I went ahead made a pull request for your update above, if you want to merge: #8.

@drazik
Copy link

drazik commented Apr 19, 2015

Does the library works with your PR @jackdp ? If yes, @kachkaev can you merge it, so we can use the library on windows and fix other bugs if there are some ?

@jackpolifka
Copy link
Contributor

@drazik No, it addresses the first issue of not being to run the command on the Windows command line. The second issue still persists though where it hangs when it tries to read errors.

@kachkaev
Copy link
Owner

@jackdp, thanks for your PR, I merged it. I agree that writing to a file instead of a pipe is a slightly odd solution, but I would not reject it right away just because it does not look ideal at first glance. Anyway I think it's pretty important to understand what's exactly going on with the pipe windows before making any changes. Please let me know if you find any clues.

@drazik
Copy link

drazik commented Apr 21, 2015

Fine, I will investigate too.

@jackpolifka
Copy link
Contributor

@drazik, can you try my fork of php-r from https://github.com/jackdp/php-r and tell me if you have any problems?

@drazik
Copy link

drazik commented Apr 28, 2015

I can't try it right now but i'll let you know asap.

@drazik
Copy link

drazik commented May 5, 2015

Hello @jackdp , I'm sorry to have let 7 days pass before testing your fork.

I have just installed it and tried the first example of the documentation :

<?php

require_once 'vendor/autoload.php';

use Kachkaev\PHPR\RCore;
use Kachkaev\PHPR\Engine\CommandLineREngine;

$r = new RCore(new CommandLineREngine('C:\\Program Files\\R\\R-3.2.0\\bin\\R.exe'));

$result = $r->run(<<<EOF
x = 1
y = 2
x + y
x + z
x - y
EOF
    );

echo $result;

Here is the error I get :

Fatal error: Uncaught exception 'Exception' with message ''pathToErrorFile' can't be found. Make sure to include it as an array argument for CommandLineFactory::createCommandLineProcess.' in D:\www\php-r\src\Kachkaev\PHPR\Engine\CommandLineREngine.php:24 Stack trace: #0 D:\www\php-r\src\Kachkaev\PHPR\Engine\AbstractREngine.php(48): Kachkaev\PHPR\Engine\CommandLineREngine->createProcess() #1 D:\www\php-r\src\Kachkaev\PHPR\Engine\AbstractREngine.php(22): Kachkaev\PHPR\Engine\AbstractREngine->createInteractiveProcess(false) #2 D:\www\php-r\src\Kachkaev\PHPR\RCore.php(18): Kachkaev\PHPR\Engine\AbstractREngine->run('x = 1\r\ny = 2\r\nx...', false, false) #3 D:\www\php-r\index.php(17): Kachkaev\PHPR\RCore->run('x = 1\r\ny = 2\r\nx...') #4 {main} thrown in D:\www\php-r\src\Kachkaev\PHPR\Engine\CommandLineREngine.php on line 24

Let me know if I have done something wrong ;)

@jackpolifka
Copy link
Contributor

Try this something like this. Make sure you have a file to write errors to.

<?php

require_once 'vendor/autoload.php';

use Kachkaev\PHPR\RCore;
use Kachkaev\PHPR\Engine\CommandLineREngine;

$args = array();
$args['pathToErrorFile'] = 'C:/path/to/errors.txt';
$r = new RCore(new CommandLineREngine('C:/Program Files/R/R-3.1.2/bin/R.exe', $args));

$result = $r->run(<<<EOF
x = 1
y = 2
x + y
x + z
x - y
EOF
    );

echo $result;

@drazik
Copy link

drazik commented May 8, 2015

I tried it, I get an error un errors.txt :

'C:/Program' n'est pas reconnu en tant que commande interne ou externe, un programme ex‚cutable ou un fichier de commandes.

I don't know what the english version of the error is, but it means "'C:/Program' is not an internal or external command, an executable program or a command file".

I tried to replace $r = new RCore(...); with

$r = new RCore(new CommandLineREngine('C:/"Program Files"/R/R-3.1.2/bin/R.exe', $args));

It solved the problem (no more error in errors.txt), but still no result in the browser. The PHP server output this error :

Maximum execution time of 30 seconds exceeded in D:\www\php-r\src\Kachkaev\PHPR\Process\CommandLine\AbstractCommandLineRProcess.php on line 54

Then I tried with a max execution time of 120 seconds, but the result (may I say "the error") is the same. I also checked that the path I give to CommandLineREngine constructor is good. Sadly I have nothing more that can help you :(

@jackpolifka
Copy link
Contributor

@drazik Using your updated CommandLineREnigne constructor still works for me.

The problem you are facing that CommandLineREnigne can't find R.exe. In your original posting, you had R-3.2.0 vs the R-3.1.3, which I'm using and you switched to for some reason. Can you update your most recent attempt to use R-3.2.0 like you did in your original posting?

Something like this:

<?php

require_once 'vendor/autoload.php';

use Kachkaev\PHPR\RCore;
use Kachkaev\PHPR\Engine\CommandLineREngine;

$args = array();
$args['pathToErrorFile'] = 'C:/your/path/to/errors.txt';
$r = new RCore(new CommandLineREngine('C:/"Program Files"/R/R-3.2.0/bin/R.exe', $args));

$result = $r->run(<<<EOF
x = 1
y = 2
x + y
x + z
x - y
EOF
    );

echo $result;

@drazik
Copy link

drazik commented May 10, 2015

@jackdp I've always been using 3.2.0 but I forgot to change the version number during a copy/paste to my github comment ;)

I'm doing exactly what you told me in your last comment and still reach max execution time. I don't think that the problem is that CommandLineREngine can't find R.exe. I fails on src\Kachkaev\PHPR\Process\CommandLine\AbstractCommandLineRProcess.php line 54 everytime. It does not seem that this line is related to finding R.exe.

@jackpolifka
Copy link
Contributor

@drazik If it's related to not to finding R.exe, I'm not sure how to reproduce the problem. While I look for another way to reproduce the problem, can you try two things for me?

1.) Check your errors.txt after you get the error related to reaching max execution time. It may hold some clues. If it says anything, please report it.

2.) Can you put this for the constructor for Kachkaev\PHPR\Engine\CommandLineREngine? I don't want to be a broken record but this is just to triple check for R.exe.

    public function __construct($rCommand, array $args = null)
    {
        if (!file_exists($rCommand)) {
            throw new \Exception($rCommand . " can't be found. Make sure you have the correct location for R.exe.");
        }
        $this->rCommand = $rCommand;
        $this->args = $args;
    }

@emiliogh
Copy link

Hi, i run the code in laravel 5.2 but only works sometimes, What could be the error?
thanks

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants