Symfony2/Symfony3 bundle for the php-r library
Add the following dependency to your project’s composer.json file:
"require": {
// ...
"kachkaev/php-r-bundle": "dev-master"
// ...
}Now tell composer to download the bundle by running the command:
$ php composer.phar update kachkaev/php-r-bundleComposer will install the bundle to vendor/kachkaev directory.
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
// ...
new Kachkaev\PHPRBundle\KachkaevPHPRBundle(),
// ...
);
}Here is the default configuration for the bundle:
kachkaev_phpr:
default_engine: command_line # default R engine (command_line is the only one currently implemented)
engines:
command_line:
path_to_r: /usr/bin/R # path to R interpreterIn most cases custom configuration is not needed, so simply add the following line to your app/config/config.yml:
kachkaev_phpr: ~$r = $container->get('kachkaev_phpr.core');
$rResult = $r->run('x = 10
x * x');
// --- or ---
$r = $container->get('kachkaev_phpr.core');
$rProcess = $r->createInteractiveProcess();
$rProcess->write('x = 10');
$rProcess->write('x * x');
$rResult = $rProcess->getAllResult();$rResult contains the following string:
> x = 10
> x * x
[1] 100
For detailed feature description of R core and R process see documentation to php-r library.
An instance of ROutputParser is also registered as a service:
$rOutputParser = $container->get('kachkaev_phpr.output_parser');
$rProcess->write('21 + 21');
var_dump($rProcess->getLastWriteOutput());
// string(6) "[1] 42"
var_dump($rOutputParser->singleNumber($rProcess->getLastWriteOutput()));
// int(42)MIT. See LICENSE.
