PHPackages                             mineria/php-r - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. mineria/php-r

ActiveLibrary

mineria/php-r
=============

Biblioteca para Hack R PHP

v1.1.0(8y ago)033[1 issues](https://github.com/gescom-sac/php-r/issues)GNUPHPPHP &gt;=5.3.9

Since Nov 30Pushed 8y ago2 watchersCompare

[ Source](https://github.com/gescom-sac/php-r)[ Packagist](https://packagist.org/packages/mineria/php-r)[ Docs](https://github.com/gescom-sac/php-r)[ RSS](/packages/mineria-php-r/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (1)DependenciesVersions (3)Used By (0)

php-r
=====

[](#php-r)

[![Donate](https://camo.githubusercontent.com/e20e56740b72c1a9d48439424e58e6bc4c8bc79270a1b723b887f56000d30a48/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f646f6e6174652d70617970616c2d626c75652e7376673f7374796c653d666c61742d726f756e646564)](https://paypal.me/kachkaev/5gbp) 🍺

PHPR (or php-r) is a library that provides ability to run R scripts from PHP. Composer package: [kachkaev/php-r](https://packagist.org/packages/kachkaev/php-r).

Optionally, the library is available [as a bundle](https://github.com/kachkaev/KachkaevPHPRBundle) for Symfony2 users.

The idea is based on invoking a command-line version of R and exchanging messages with this external process. Integration with server-based implementation of R can be easily implemented on demand as the library architecture supports that.

It is possible to both run all R code in one batch and interactively exchange commands with R interpreter.

Usage
-----

[](#usage)

### Option 1: all R code in one batch

[](#option-1-all-r-code-in-one-batch)

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

$r = new RCore(new CommandLineREngine('/path/to/R'));

$result = $r->run( x + y
[1] 3
> x + z
Error: object 'z' not found
> x - y
[1] -1

```

Method `run()` is always called in a clean scope of R variables, i.e. the following usage will result an R error:

```
echo $r->run("x = 100")
echo "\n=====\n"
echo $r->run("x * x")
```

PHP output:

```
> x = 100
=====
> x * x
Error: object 'x' not found

```

### Option 2: interactive exchange of data

[](#option-2-interactive-exchange-of-data)

To exchange commands with a single R process interactively, another approach should be used:

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

$r = new RCore(new CommandLineREngine('/path/to/R'));
$rProcess = $r->createInteractiveProcess();
$rProcess->start();
$rProcess->write('x = 100');
// Do something else
$rProcess->write('x * x');

echo $rProcess->getAllResult();
```

PHP output:

```
> x = 100
> x * x
[1] 10000

```

The process is synchronous, i.e. if R code sent to `write()` implies some complex computations, PHP will wait until they are finished.

Multiple commands can be passed to R inside one `write()`; they can be multi-line too:

```
$rProcess->write(createInteractiveProcess();
$rProcess->start();
$rProcess->write(getLastWriteErrors();
echo $errors[0]->getErrorMessage()
// object 'xxx' not found
echo $errors[0]->getCommand()
// x + xxx

$rProcess->getAllInput();
$rProcess->getAllOutput();
$rProcess->hasErrors();
$rProcess->getErrorCount();
$rProcess->getErrors();
```

Passing `true` to `get(LastWrite|All)Input/get(LastWrite|All)Output/get(LastWrite|All)Result` splits strings into arrays, where each element corresponds to a single command:

```
$rProcess = $r->createInteractiveProcess();
$rProcess->start();
$rProcess->write(getAllResult(true);
// [
//   ['x ={newline}1 + 1', '', null],
//   ['y', null, 'Error: object \'y\' not found'],
//   ['x', '2', null]
// ]
```

#### Sensitivity to R errors

[](#sensitivity-to-r-errors)

If it is necessary to make sure that a sequence of R commands is running with no errors, and calling `hasLastWriteErrors()` after each `write()` is unreasonable, the process can be made sensible to errors. `RErrorsException` will be thrown on `write()`:

```
$rProcess->setErrorSensitive(true);
$rProcess->write('x = 1 + missingVariable');
// RErrorsException
```

This is the same as:

```
$rProcess->setErrorSensitive(false);
$rProcess->write('x = 1 + missingVariable');
if ($rProcess->hasLastWriteErrors()) {
    throw new RErrorsException($rProcess->getLastWriteInput(true), $rProcess->getLastWriteOutput(true), $rProcess->getLastWriteErrors());
}
```

R-related errors and the exception thrown are not critical; the same instance of R process can be still used after they occur. If last input contains multiple commands, and several of them cause errors, `RErrorsException` will have the complete list. In any case all commands passed to `write()` will be attempted by R interpreter.

```
$allErrors = $rErrorsException->getErrors();
if (count ($allErrors) > 1) {
    $secondError = $allErrors[1];
    echo $secondError->getErrorMessage();
}
```

#### Parsing R output

[](#parsing-r-output)

To ease parsing of R output, `ROutputParser` can be used:

```
use Kachkaev\PHPR\ROutputParser;

$rOutputParser = new ROutputParser();
$rProcess->write('21 + 21');
var_dump($rProcess->getLastWriteOutput());
// string(6) "[1] 42"
var_dump($rOutputParser->singleNumber($rProcess->getLastWriteOutput()));
// int(42)
```

See PHPdoc annotations to classes for more details.

License
-------

[](#license)

MIT. See [LICENSE](LICENSE).
============================

[](#mit-see-license)

php-r
=====

[](#php-r-1)

Install Composer:

composer require mineria/php-r
==============================

[](#composer-require-mineriaphp-r)

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 71.4% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~1 days

Total

2

Last Release

3081d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/545561b784e262fc902e268818f8f6b3c6e149631c6eaa61edb2c2852b77e1f1?d=identicon)[gescomsac](/maintainers/gescomsac)

---

Top Contributors

[![jvasquezv](https://avatars.githubusercontent.com/u/4551379?v=4)](https://github.com/jvasquezv "jvasquezv (5 commits)")[![gescomsac](https://avatars.githubusercontent.com/u/6846761?v=4)](https://github.com/gescomsac "gescomsac (2 commits)")

### Embed Badge

![Health badge](/badges/mineria-php-r/health.svg)

```
[![Health](https://phpackages.com/badges/mineria-php-r/health.svg)](https://phpackages.com/packages/mineria-php-r)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
