【问题标题】:'Rscript' is not recognized as an internal or external command, operable program or batch file'Rscript' 不是内部或外部命令、可运行程序或批处理文件
【发布时间】:2016-08-10 09:12:32
【问题描述】:
shell_exec("Rscript C:\R\R-3.2.2\bin\code.R ");

这是对脚本的调用。在调用上述脚本时,出现错误。

我正在尝试从上述路径调用我的 R 脚本,但没有显示任何输出。在检查 PHP 的错误日志时,它说 'Rscript' 不是内部或外部命令、可运行程序或批处理文件。该脚本在 Rstudio 上运行良好,但不能在命令行上运行。

【问题讨论】:

  • 请更具描述性,并附上您遇到的错误
  • 您可以在命令提示符下尝试类似的操作:cd C:\R\R-3.2.2\bin\ & Rscript "C:\R\R-3.2.2\bin\code.R",或者可能:cd C:\R\R-3.2.2\bin\ & Rscript code.R。基本上,使用cd 将目录更改为Rscript.exe 的位置,然后使用& 运行第二个命令,其中包含Rscript 和脚本路径。
  • 另一个选项是包含路径作为 Rscript 命令的一部分:"C:\R\R-3.2.2\bin\Rscript" C:\R\R-3.2.2\bin\代码.R
  • @Dave2e 谢谢....解决了问题:D

标签: php r shell cmd path


【解决方案1】:

在 Windows 中将 Rscript 路径添加到您的环境变量中:

进入控制面板\系统和安全\系统,点击高级系统设置,然后环境变量,点击下框路径,编辑,添加“C:\R\R-3.2.2\bin”

重新启动一切。去应该不错。那么你应该可以做到

exec('Rscript PATH/TO/my_code.R')

而不是输入Rscript 的完整路径。如果您的 php 文件位于同一目录中,则不需要 my_code.R 脚本的路径。

【讨论】:

    【解决方案2】:

    您需要设置 RScript.exe 程序所在的正确路径。

    exec ("\"C:\\R\\R-3.2.2\\bin\\Rscript.exe\"
               C:\\My_work\\R_scripts\\my_code.R my_args";
    
    #my_args only needed if you script take `args`as input to run
    

    另一种方法是在 r 脚本 (my_code.r) 中声明标头

     #!/usr/bin/Rscript
    

    从命令行调用它

    ./my_code.r 
    

    【讨论】:

      【解决方案3】:

      如果您在 Git Bash 终端中运行它,您可以按照 @user5249203 建议的修改版本:在文件 my_code.R 的第一行中,输入以下内容

      #!/c/R/R-3.2.2/bin/Rscript.exe
      

      我假设您的 Rscript.exe 路径是上面列出的 C:\R\R-3.2.2\bin。对于在 Windows 中具有不同 Rscript.exe 路径的任何人,只需相应地修改 Rscript 的路径。修改 R 代码后,您可以使用 path-to-the-code/mycode.R 在 Git Bash 终端中运行它。我已经在我的电脑上测试过了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-08-25
        • 2014-06-01
        • 2022-01-06
        • 2016-07-07
        • 2021-12-20
        • 2016-12-03
        • 2019-04-25
        • 2015-05-16
        相关资源
        最近更新 更多