3

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

3
  • the R installation location is irrelevant right now. the RCore class isn't loaded, so there's no way for the code to work. Commented Apr 20, 2016 at 16:32
  • composer require kachkaev/php-r is the easiest way to install php-r Commented Apr 20, 2016 at 16:34
  • 1
    I never received any feedback regarding the answer I left; did it solve your problem? If it did, marking it as accepted would be appreciated; thanks. Commented Apr 23, 2016 at 17:04

1 Answer 1

2
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"
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.