Goal: Run a simple Rscript from a wordpress page.
Setup:
PHP code (within wordpress Page)
<?php
echo "This is the Exec-PHP 'Hello World'\n";
echo exec("date");
?>
<?php
exec("Rscript <PATH>/test.R");
?>
<img src="<Image Location>/samplePlot.png" alt="" title="Sample R" />
Rscript - test.R
png( "<Image Location>/samplePlot.png")
hist( sample( 1:10, 100, replace = TRUE), main= Sys.time(), lwd = 5)
dev.off()
The image file loads but it is not updated, indicating the Rscript was never executed. I've isolated it down to that being the issue but unsure why that is.
How can I debug this? I don't really know any PHP but I tried the following:
<?php
exec("\usr\bin\Rscript <PATH>/test.R", $output, $result);
echo $output;
echo $result
?>
Which returns:
Array2
I was hoping to get the command line output to check for errors. Is this possible?
I would think that the problem is that you did not specify the full path to Rscript and the user running PHP/Apache just does not know where to search for it.
Update that exec command like (on Linux):
exec("/usr/bin/Rscript <PATH>/test.R");
Anyway, I would suggest installing littler for the task later and (based on that) runnning r instead of Rscript for letting things run a lot faster - if installing eg. rApache is not an option.
To get an idea of the problem try:
$e = exec("\usr\bin\Rscript <PATH>/test.R 2>&1");
var_dump($e);
If you get something like: Error in dyn.load(file, DLLpath = DLLpath, ...) ... you probably need to update the servers dynamic libraries (try searching for libfreetype.dylib), or if your running MAMP (as I am) you need to comment (#) the two uncommented lines in: /Applications/MAMP/Library/bin/envvars
I know it is a long time since you posted the question, but I spend a lot of time with a similar problem - hopefully somebody can save some time ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With