Run R in php under ubuntu

2019-05-10 07:18发布

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.

1条回答
Juvenile、少年°
2楼-- · 2019-05-10 07:50

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.

查看更多
登录 后发表回答