1

I am trying to execute R script from Php. It works fine with the command prompt with this :

 Documents\R\R-3.5.0\bin\R.exe Desktop\my_script.R

But the same is not working from PHP call using exec. Any suggestion please!

<?php 
   exec("Documents\R\R-3.5.0\bin\R.exe Desktop\my_script.R", $output);
   print_r($output);
?>

print_r($output) displays only "Array()" no correct result ;

5
  • try C:\Users\UserName\Documents ... etc. Commented Nov 6, 2018 at 12:26
  • Hi @ThisGuyHasTwoThumbs, I tried it already, no luck, Commented Nov 6, 2018 at 12:35
  • place ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(-1); before the exec() and see if it outputs any errors Commented Nov 6, 2018 at 12:36
  • Thanks! I get an error saying "The system cannot find the path specified." I am using this below C\Users\"myName"\Documents\R\R-3.5.0\bin\R.exe Desktop\my_script.R may be it might need pwd? same path is what I am getting a result in cmd prompt Commented Nov 6, 2018 at 12:44
  • not sure with Windows machines ... see if it works if the .R script is in the same place as the exec script (and change path accordingly) Commented Nov 6, 2018 at 12:45

1 Answer 1

1

Those paths must be relative to the php working directory. Otherwise they must be fully specified:

Try:

$cmd = "C:\Users\YOURNAME\Documents\R\R-3.5.0\bin\Rscript.exe C:\Users\YOURNAME\Desktop\my_script.R";
exec($cmd, $output);
Sign up to request clarification or add additional context in comments.

12 Comments

I get this error with the proper path now: Array ( [0] => ARGUMENT 'Desktop\my_script.R' ignored [1] => [2] => [3] => R version 3.5.0 (2018-04-23) --
Do you have a program called Rscript in C:\Users\YOURNAME\Documents\R\R-3.5.0\bin\?
Yes, I do have RScript in the same path. I use the same from cmd prompt and I get answer correctly..
Try the updated answer - if it still doesn't work let me know
I get nothing. No error message in php error_log file. my_script.R file creates two files 1.Rdata 2. plot png file Also I tried to keep this my_script.R file from different folders like Desktop folder, document folders also used C:\wamp64\www\my_script.R.... all produces nothing.... but works fine from cmd prompt. But i need to execute this script from php. Thanks for your time.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.