目标: 从 wordpress 页面运行一个简单的 Rscript。
- 我目前正在尝试在加载页面时使用 exec() 运行 Rscript。该脚本从 1 到 10 的整数中创建 100 个随机样本的直方图,将系统时间写入标题并将图形保存到 .png 文件。
设置:
- 在 Ubuntu EC2 微型实例上运行 Wordpress 安装
- R已经通过ssh成功安装和测试
- 使用 Exec-PHP Wordpress 插件编写和执行 PHP 代码(测试成功)
PHP 代码(在 wordpress 页面内)
<?php
echo "This is the Exec-PHP 'Hello World'\n";
echo exec("date");
?>
<?php
exec("Rscript <PATH>/test.R");
?>
<img src="<Image Location>/samplePlot.png" alt="" title="Sample R" />
Rscript - test.R
png( "<Image Location>/samplePlot.png")
hist( sample( 1:10, 100, replace = TRUE), main= Sys.time(), lwd = 5)
dev.off()
图像文件加载但未更新,表明 Rscript 从未执行过。我已将其归结为问题所在,但不确定为什么会这样。
我该如何调试它?我真的不知道任何 PHP,但我尝试了以下方法:
<?php
exec("\usr\bin\Rscript <PATH>/test.R", $output, $result);
echo $output;
echo $result
?>
哪个返回:
Array2
我希望获得命令行输出以检查错误。这可能吗?
最佳答案
我认为问题在于您没有指定 Rscript
的完整路径,而运行 PHP/Apache 的用户只是不知道在哪里搜索它。
更新 exec
命令(在 Linux 上):
exec("/usr/bin/Rscript <PATH>/test.R");
无论如何,我建议安装 littler为稍后的任务和(基于此)运行 r
而不是 Rscript
让事情运行得更快 - 如果安装例如。 rApache不是一个选项。
关于php - 从 wordpress 安装运行 exec() Rscript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8481790/