Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Php R Bundle Laravel Package

kachkaev/php-r-bundle

A Symfony bundle that brings R statistical computing into PHP applications, enabling you to run R scripts, pass data between PHP and R, and incorporate R-powered analysis into Symfony projects.

View on GitHub
Deep Wiki
Context7

Getting Started

  1. Install R on the host (not just a Composer package). Verify the web/PHP user can execute it:
    which R
    R --version
    sudo -u www-data R --version
    
  2. Install the bundle (this is a Symfony 2/3-era package):
    composer require kachkaev/php-r-bundle
    
    It depends on kachkaev/php-r, which provides the actual RClient.
  3. Register the bundle in app/AppKernel.php:
    new Kachkaev\PHPRBundle\KachkaevPHPRBundle(),
    
  4. Configure the R binary path in app/config/config.yml:
    kachkaev_php_r:
        r_binary: /usr/bin/R
    
    Check the bundle's DependencyInjection/Configuration.php and Resources/config/services.yml for the exact config keys and service ID — the bundle exposes the client as a service (commonly aliased to kachkaev_php_r or the RClient class).
  5. Look first at Resources/config/services.yml to confirm the service ID and whether it is public. Then inject it:
    use Kachkaev\PHPR\RClient;
    
    class ReportController
    {
        public function summaryAction()
        {
            /** @var RClient $r */
            $r = $this->get('kachkaev_php_r'); // or type-hint RClient if autowiring is enabled
    
            $result = $r->run('mean(c(1, 2, 3, 4))');
    
            return new Response($result->getOutput());
        }
    }
    
  6. First real use case: move an existing analytical calculation (averages, regressions, forecasting, chart generation) out of PHP and into an R script, then call it from a Symfony service/controller and render or persist the result.

Implementation Patterns

  • One client per request, many R statements. The client wraps an R process/session. Assign variables and run multiple statements on the same client instead of spawning a new process per expression:
    $r->run('values <- c(10, 20, 30, 40)');
    $r->run('avg <- mean(values)');
    echo $r->run('avg')->getOutput();
    
  • Use JSON/CSV as the PHP↔R contract. Text output from R includes [1] prefixes and formatting noise. Prefer jsonlite in R and json_decode in PHP, or write/read a temp CSV:
    $script = <<<'R'
    library(jsonlite)
    toJSON(list(mean = mean(values), sd = sd(values)), auto_unbox = TRUE)
    R;
    $data = json_decode($r->run($script)->getOutput(), true);
    
  • Pass data via files or escaped variables, never raw interpolation. For user-supplied values, write to a temp file and read.csv()/readRDS() inside R, or carefully quote/escape values before assigning R variables.
  • Wrap the client in your own service. Keep RClient out of controllers; create e.g. AppBundle\Analytics\RStatsService that owns script generation, error handling, and result parsing. This makes testing and later replacement much easier.
  • Use R for file-producing tasks. Charts are a common fit: call png('/tmp/chart.png'), build the plot, dev.off(), then serve/attach the file from PHP.
  • Handle R failures explicitly. Check output, stderr, and exceptions around each run() call. Log the generated script and R output so failures are reproducible.
  • Test by stubbing the R service. Unit-test your wrapper with a fake implementation; keep a small integration test suite that requires R installed in CI.

Gotchas and Tips

  • Archived and old. Last release was 2017, zero dependents, and it targets Symfony 2/3 and PHP 5/7-era APIs. Expect incompatibilities with Symfony 4+ and PHP 8. Pin versions, or fork/copy the tiny service definition into your own project.
  • Requires proc_open/process execution. Many hosts disable proc_open, restrict open_basedir, or run PHP-FPM as a user without access to /usr/bin/R. Test the exact runtime user, not your shell user.
  • Performance is process-bound. Every request that touches R pays process startup cost. Cache results, push heavy work to a queue/worker, or use a long-lived R server (Rserve) instead of CLI invocation for high traffic.
  • Security: treat R script generation like SQL injection. R can call system(), read files, and execute shell commands. Never concatenate raw request data into a script. Whitelist operations and pass parameters through files or strictly escaped variables.
  • Output parsing is fragile. getOutput() returns R's printed representation, not typed PHP data. Normalize whitespace and [1] prefixes, and prefer JSON for anything non-trivial.
  • Path differences. Linux/macOS use /usr/bin/R; Windows uses Rscript.exe. Make the binary path environment-specific config, not hard-coded.
  • Debugging workflow: log the generated R script, run it manually with Rscript script.R, and compare stderr/exit codes. Most failures are missing R packages, wrong working directory, or permissions.
  • Migration tip: because the bundle is thin, replacing it with a symfony/process wrapper (run Rscript --vanilla script.R) is usually straightforward and removes a dead dependency.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
abbasudo/artisan-route
mirvan/ippanel
ignibyte/aic-framework
glavweb/data-schema-bundle
glavweb/datagrid-bundle
tacticmedia/scim-server
uni-method/frame
neika-ai/sdk
adachsoft/php-code-style
pharaonic/laravel-notification-dependencies
beautinow/translug
dev-brainiac/bot-cloaked
octoberrain/html
octoberrain/extension
octoberrain/exception
octoberrain/contracts
adachsoft/api-version-bundle
cgv/localized-resource
noith/settings
kda/laravel-package