【问题标题】:Rscript behaving differently when run from commandline and from PHP exec从命令行和 PHP exec 运行时,Rscript 的行为不同
【发布时间】:2015-04-12 23:44:37
【问题描述】:

您好,我正在使用 PHP exec 运行 Rscript,它的行为很奇怪.......它启动了 R,但在以下行抛出错误:-

filein = filein[,c("id","bank","trans_date","description","description_2",
                 "description_3","description_4","description_5" ,"type",
                 "debit","credit","statement_balance", "cleared_balance",
                 "debit_int_rate","credit_int_rate","category")]

这个简单的重新排列数据集中的列。

它会抛出以下错误:-

错误

[.data.frame`(filein, , c("id", "bank", "trans_date", "description", 
:  undefined columns selected

但是我从命令行运行相同的脚本,它运行时没有任何错误。

(我正在使用 PHP5 的 ubuntu 14.04 机器上运行 Rscript……当我在 Windows 机器上使用 PHP 运行相同的脚本时,它也可以完美运行)

有人知道为什么会这样吗?

【问题讨论】:

  • 当您使用 PHP5 导入数据框时,您确定“_”字符不会以不同的方式被读取吗? -- 通过添加“print colnames(filein)”来检查列是如何命名的。我认为这可能是一个文本解析问题,它改变了列的名称。
  • PHP 在与 Rscript 不同的权限下运行,因此它可能无法访问该文件。
  • @LauriK 更准确地说,web 服务器 运行在与 用户 不同的权限下(换句话说,如果 PHP 脚本由控制台上的用户php 将以与Rscript 完全相同的权限运行)。但要点是一样的。
  • @KonradRudolph 谢谢!我的 web server sysadmin 职业持续了整整两个月,所以我有一些基础知识,可以调试权限和依赖问题,但是我还没有系统的理解。
  • 同意这可能是文件权限问题。尝试在读取文件时添加检查以查看它是否读取任何内容... if(nrow(dataframe) == 0)stop("dataframe not read in proper")

标签: php r ubuntu rscript


【解决方案1】:

感谢您的所有帮助....所以这不是权限问题(我之前已经修复了这些问题).......问题是:----PHP传递给命令行的命令执行是 sudo /Rscript /home/xin/Documents/ClassificationApp/ClassificationAllInOne.R "http://localhost/categorisation/public/classification/data/1423746975.json"

但是,R 收到的参数是“localhost/categorisation/public/1423746975.json2”;

由于某种原因,Ubuntu/PHP 组合在参数字符串的末尾添加了一个 2....我在 ClassificationAllInOne.R 中添加了一行来去除 2,它运行良好

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-18
    • 2015-11-03
    • 1970-01-01
    • 2011-03-30
    • 2019-02-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多