Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run exec() Rscript from wordpress install

Tags:

php

r

amazon-ec2

Goal: Run a simple Rscript from a wordpress page.

  • I'm currently attempting to run an Rscript using exec() upon loading the page. The script creates a histogram of 100 random samples from integers 1 through 10, writes system time to title and saves figure to .png file.

Setup:

  • Running Wordpress install on Ubuntu EC2 micro instance
  • R has been successfully installed and tested through ssh
  • Using Exec-PHP Wordpress plugin so that PHP code can be written and executed (tested successfully)

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?

like image 709
bnjmn Avatar asked May 30 '26 00:05

bnjmn


2 Answers

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.

like image 149
daroczig Avatar answered May 31 '26 13:05

daroczig


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 ;)

like image 24
seb5s Avatar answered May 31 '26 14:05

seb5s