Run exec() Rscript from wordpress install


Run exec() Rscript from wordpress install

目标:从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" />

脚本 - 测试。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 不是一种选择。

要了解问题,请尝试:

$e = exec("'usr'bin'Rscript <PATH>/test.R 2>&1");
var_dump($e);

如果你得到类似的东西:dyn.load(file, DLLpath = DLLpath, ...(您可能需要更新服务器动态库(尝试搜索libfreetype.dylib(,或者如果您正在运行的MAMP(就像我一样(,则需要注释(#(中的两个未注释行:/Applications/MAMP/Library/bin/envvars

我知道自从你发布这个问题以来已经有很长时间了,但我花了很多时间在类似的问题上 - 希望有人可以节省一些时间;)