简体   繁体   中英

How to run R through PHP with exec?

I am going to ask something, that I know that it has been asked already some times. But since, all of the past posts are quite old and none of them answer my problem..I try again. I am completely new in R language and relative new in php. What I want to do is to use the exec() function from php in order to execute a R script. Most of the people here will start talking about rapache, rserve and I don't know what else..but since I am not familiar with all these technologies, I prefer just using exec.

The code I will show here is working just fine when I run it with Rscript from the terminal. # R script png("temp.png") plot(5,5) dev.off()

But when I try to run it either with Rscript or with R CMD BATCH from PHP, like this:

       echo exec("Rscript my_rscript.R"); //OR
       //echo exec("R CMD BATCH my_rscript.R");

I get nothing back.

I have checked if exec() function is available and if it works. Everything is ok with this. I read also, that I might have to change the permissions of the webserver...but I don't know how to do this in mamp.

I hope I am clear with my problem and someone can help.

Thanks Dimitris

Not sure if this works for images, but I got nothing back until I stored the results in the second parameter of the exec() function

exec("Rscript test.R $N", $results);
print_r($results);

This worked for me to get the image saved to directory and visible on the web page. From the command line:

chown www-data:www-data /usr/bin/Rscript
chown www-data:www-data -R /PATH/TO/RSCRIPTFOLDER

Changed permissions first on the Rscript executable, and then did the same for the entire folder that houses the .php file, .R file, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM