I want to use R inside PHP using php-r. As I am very new to PHP, I would be very grateful if anyone guides me on how to install php-r inside xampp and run the same.
I tried putting the PHPR directory inside htdocs and accessed it from php using
<?php
use Kachkaev/PHPR/RCore;
use Kachkaev/PHPR/Engine/CommandLineREngine;
$r = new RCore(new CommandLineREngine('/usr/bin/R'));
$result = $r->run(<<<EOF
x = 1
y = 2
x + y
x + z
x - y
EOF
);
echo $result;
?>
This returns an error
Fatal error: Class 'Kachkaev\PHPR\RCore' not found in /opt/lampp/htdocs/testserver/test.php on line 6
N.B: My R is installed in /usr/bin/R
use Kachkaev/PHPR/RCore;
use Kachkaev/PHPR/Engine/CommandLineREngine;
I suspect you are confusing the use statement's purpose. It imports a namespace into the current code. Accordingly, it uses the namespace separator -- a backslash, not a forward slash:
use Kachkaev\PHPR\RCore;
use Kachkaev\PHPR\Engine\CommandLineREngine;
Before you can import the namespace, PHP has to be aware of the associated code. The most basic way is to use include() or require() statements:
require /path/to/htdocs/php-r/src/Kachkaev/PHPR/RCore.php
Note that these are filesystem paths, not URLs. require will throw an error if the file cannot be found, include will not.
Most modern projects have an autoloading component such as Composer that will handle this for you though. Once set up, you would simply add this to your composer.json file:
{
"require": {
"kachkaev/php-r": "dev-master"
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With