okvpn/php-rdk

整合PHP和R

1.0.1 2019-01-13 15:04 UTC

This package is auto-updated.

Last update: 2024-09-14 03:40:58 UTC


README

整合PHP和R

目的

有时需要执行复杂的计算和分析数据,但您的应用程序是用PHP编写的。众所周知,PHP不是用于统计计算和数据管理的优秀编程语言。但R是一种用于统计计算和图形的环境。因此,这个库提供了R和PHP之间的集成。

安装

composer require okvpn/php-rdk
  • 安装R
sudo apt-get install r-base

运行命令以确认R已成功安装。

R --version

Output:

R version 3.2.3 (2015-12-10) -- "Wooden Christmas-Tree"
Copyright (C) 2015 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under the terms of the
GNU General Public License versions 2 or 3.
For more information about these matters see
https://gnu.ac.cn/licenses/.

示例

运行R脚本

use Okvpn\R\Process\RProcess;
use Okvpn\R\UnixPipes;

$rPath = '/usr/bin/R'; //path to R
$process = new RProcess(new UnixPipes(), $rPath);
$process->start();

$rOutput = $process->write(<<<TXT
library("plot3D")
ii = which(Hypsometry\$x > -50 & Hypsometry\$x < -20)
jj = which(Hypsometry\$y > 10 & Hypsometry\$y < 40)
zlim = c(-10000, 0)
png('test.png')
persp3D(z = Hypsometry\$z[ii,jj], xlab = "longitude", bty = "bl2", ylab = "latitude", zlab = "depth", clab = "depth, m", expand = 0.5, d = 2, phi = 20, theta = 30, resfac = 2, contour = list(col = "grey", side = c("zmin", "z")), zlim = zlim, colkey = list(side = 1, length = 0.5))
dev.off()
TXT
);

print_r($rOutput->getAllOutput());

输出图像/tmp/test.png

Image

使用表达式构建器

use Okvpn\R\Process\ProcessManager;
use Okvpn\R\Types\Type;

$rProcess = ProcessManager::create();
$exprBuilder = $rProcess->createExpressionBuilder();

$result = $exprBuilder
    ->select('t(combn(c(0:3), :b))', Type::MATRIX)
    ->setParameter('b', $b)
    ->execute()->getSingleResult();

许可证

MIT许可证。请参阅LICENSE