'Rscript' is not recognized as an internal or external command, operable program or batch file

20,909

Solution 1

Add the Rscript path to your environment variables in Windows:

Go to Control Panel\System and Security\System and click Advanced System Settings, then environment variables, click on path in the lower box, edit, add "C:\R\R-3.2.2\bin"

Restart everything. Should be good to go. Then you should be able to do

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

instead of typing the full path to Rscript. Won't need the path to your my_code.R script if your php file is in the same directory.

Solution 2

You need to set the proper path where your RScript.exe program is located.

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

other way is you declare header in your r script (my_code.r)

 #!/usr/bin/Rscript

and call it from command line

./my_code.r 
Share:
20,909
Hemant Pandey
Author by

Hemant Pandey

Updated on November 18, 2021

Comments

  • Hemant Pandey
    Hemant Pandey over 2 years
    shell_exec("Rscript C:\R\R-3.2.2\bin\code.R ");
    

    This is the call to script.On calling the above script, the error occurs.

    I am trying to call my R script from the above path but no output is being shown. While checking the error logs of PHP, it says 'Rscript' is not recognized as an internal or external command, operable program or batch file.' The script is working fine on the Rstudio but not running on the command line.

    • georoot
      georoot about 8 years
      Please be more descriptive and also attach the error that you are getting
    • Oliver Frost
      Oliver Frost about 8 years
      You could try something like this in the command prompt: cd C:\R\R-3.2.2\bin\ & Rscript "C:\R\R-3.2.2\bin\code.R", or possibly: cd C:\R\R-3.2.2\bin\ & Rscript code.R. Basically, use cd to change directory to the location of your Rscript.exe, then run a second command using & with the Rscript and the path to your script.
    • Dave2e
      Dave2e about 8 years
      Another option is include the path as part of the Rscript command: "C:\R\R-3.2.2\bin\Rscript" C:\R\R-3.2.2\bin\code.R
    • Hemant Pandey
      Hemant Pandey about 8 years
      @Dave2e Thanks....that solved the problem :D