mrwooster
mrwooster

Reputation: 24227

Cant run R command from PHP. Paths issue?

I am trying to run an R command from PHP using exec. I know that it would make much more sense to use RServer, or a socket connection, but those options are currently not available to me.

The code looks like this:

$cmd = '/var/www/r.sh';
exec($cmd, $out, $return_var);

r.sh contains the following

#!/bin/bash
source /home/ubuntu/.bashrc
cd /home/ubuntu
R CMD BATCH RFile.R

When I the command from the shell, it works absolutely fine. When I run the command from PHP, I get the following error:

> test( read.csv("OutData.csv",header=T,stringsAsFactors=FALSE,encoding="UTF-8"))
Error in library(randomForest) :
  there is no package called 'randomForest'
Calls: test -> library
Execution halted

My initial thought was that it might be permission related. I have given www-data read an execute permission to everything to no avail. I have also run the command from the shell as the www-data user and it works fine. Its only when invoked from PHP that it fails.

So, I am now thinking that its a paths issue. randomForest is an R library and is correctly installed on the box:

ubuntu@<removed>:/var/www$ sudo find / -name randomForest
/home/ubuntu/R/x86_64-unknown-linux-gnu-library/3.0/randomForest
/home/ubuntu/R/x86_64-unknown-linux-gnu-library/3.0/randomForest/R/randomForest

I can't find any shell environment variables which set up paths, so I don't understand why the command works from the shell, but not from PHP.

Any ideas?

Upvotes: 2

Views: 303

Answers (1)

Ben Bolker
Ben Bolker

Reputation: 226247

The relevant environment variables are R_LIBS and R_LIBS_USER; the R Installation and Administration Manual is the relevant reference. If you want to see how these variables are set from within an R session, try

ss <- Sys.getenv()
ss[grep("^R_LIB",names(ss))]

Upvotes: 2

Related Questions