如何从PHP代码执行R程序?

I have an R program that executes properly from the command line using both "R CMD BATCH file.r" and "Rscript file.r". If I try to execute via PHP code, I am getting odd outcomes. Here's my results:

$lastline = system("R CMD BATCH file.r", $returnval);

**$returnval = 133, R code creates an Rout output file, R code doesn't seem to have been fully executed as there are SQL records that should have been created

$lastline = system("Rscript file.r", $returnval);

**nothing is ever returned and the browser just spins indefinitely

I have also tried using exec_shell and exec. I get the same results. Any thoughts on how to effectively execute the R code via PHP? From what I've read, the "R CMD BATCH" option might be more promising, however, it does not appear that the work being done by R/RMySQL is completing. Not sure if that's the issue or something else. But I do know that the R program works fine executing via command line.

Also, I'd prefer to not have to use any Apache or PHP modules to accomplish this. Thanks for your input.

I would recomend using Rserve to run the R code. Rserve is a TCP/IP server that runs in the background. There is a module for it (rserve-php), but you can recreate the functionality.

You can also use sockets with the make.socket() function that is internal to R.