2

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?

4
  • from within R, try ss <- Sys.getenv(); ss[grep("^R_",names(ss))], or ss[grep("^R_LIB",names(ss))] Commented Nov 11, 2013 at 1:52
  • What should I be seeing here? Looks like ss is populated with all the env data from the shell. Unfortunately my experience with R is very very limited so I am not sure what I should be looking for. Commented Nov 11, 2013 at 1:54
  • 3
    R_LIBS, R_LIBS_USER. (The "R Installation and Administration Manual", e.g. cran.stat.sfu.ca/doc/manuals/R-admin.html , is the reference for this stuff.) Commented Nov 11, 2013 at 2:22
  • Thanks, this is exactly what I needed. If you will put the above in an answer, I can accept it :) Commented Nov 11, 2013 at 18:51

1 Answer 1

2

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))]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.