Take the 2-minute tour ×

I have an R script that runs just fine when I run it from the command line and saves an image to the file system. However when I run it from the php script using the following command :

exec("cat my_rscript.R | /usr/bin/R --vanilla");

It doesn't run. Note that when I'm running it in the command line I'm using the exact same call. I'm pretty sure that I'm in the right directory (the php file and the R script are in the same directory). What could be the cause me not being able to run the R script from php? I think this has to do with the fact that Im running R-64 bit and php is only 32 bit, however Im not sure if thats the problem.

share|improve this question
My first guess is the user you're executing the PHP call from doesn't have access to one or more of the resources (filesystem-wise). –  Jared Farrish Nov 5 '11 at 22:31
Do you mean access to the R script or the folder? –  jkjk Nov 5 '11 at 22:37
Both. If the user (apache?) can't access either or, then you probably won't have any output to get. –  Jared Farrish Nov 5 '11 at 22:39
How can i give full access to the user? Can you point me to some tutorials? –  jkjk Nov 5 '11 at 22:43
He he. Yeah, I've not really had to mess with this very much, and it's been a while since I had to work on it. Maybe start here? I'm probably not much use on an answer (hence, my comment). –  Jared Farrish Nov 5 '11 at 22:47

1 Answer 1

up vote 1 down vote accepted

I'd recommend the following steps to debug this, all via the commands to the shell, from PHP:

  1. Get the output of which R (make sure that it's in the path) (e.g. which R > where_is_r.txt
  2. Get the location of where your shell begins execution, i.e. the path from which R is being called (that should be the location of the "where_is_r.txt" file)
  3. Create a simple helloworld.r (e.g. writes normally distributed random values to a text file), and execute Rscript --vanilla /path/to/helloworld.r
  4. Replace helloworld.r with your script, and execute Rscript --vanilla /path/to/my_script.R

If these don't reveal where the problem is, you may need to save the output of error (STDERR) and output (STDOUT). This can be done inside R, but it's better to do this via bash. See this page for a guide on how to do this redirection.

Also, in general it is best to use fully specified paths rather than depend on the shell being invoked in a particular directory. The full specification could be either an absolute or relative path, but relative paths and symlinks are a little harder to debug than absolute paths.

share|improve this answer

Your Answer


