texlab / r
Running R code from PHP
v0.03
2020-10-21 22:42 UTC
Requires
- php: >=7.4
Requires (Dev)
- phpmd/phpmd: ^2.9
- phpstan/phpstan: ^0.12.32
- phpunit/phpunit: ^9.2
- squizlabs/php_codesniffer: 3.*
- vimeo/psalm: ^3.12
This package is auto-updated.
Last update: 2024-10-29 06:05:51 UTC
README
install
composer require texlab/r
usage example
PHP code
<?php header("Content-Type: text/plain"); use TexLab\R\Runner; require "../vendor/autoload.php"; $r = new Runner(); $code = <<<R x=1:10 y=5+1*x+rnorm(10) summary(lm(y~x)); R; echo $r->run($code);
output
Call:
lm(formula = y ~ x)
Residuals:
Min 1Q Median 3Q Max
-1.1988 -0.9164 -0.1257 0.7085 1.7562
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 4.5280 0.7291 6.211 0.000256 ***
x 1.0529 0.1175 8.961 1.91e-05 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 1.067 on 8 degrees of freedom
Multiple R-squared: 0.9094, Adjusted R-squared: 0.8981
F-statistic: 80.29 on 1 and 8 DF, p-value: 1.914e-05
PHP code
<?php use TexLab\R\Script; use TexLab\R\Runner; require "../vendor/autoload.php"; $r = new Script(new Runner()); $code = <<<R library(base64enc) temp_file_name <- tempfile() png(file = temp_file_name, width = 300, height = 300, units = "px"); hist(x=rnorm(1000), col='blue'); dev.off(); php_plot_data <- paste("data:image/png;base64,", base64enc::base64encode(temp_file_name)) file.remove(temp_file_name) R; $data = $r->run($code) ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <img src="<?= $data['php_plot_data'] ?>"> </body> </html>