Calling R Scripts from PHP (mostly a note to myself)
I’m writing this down here so that I have a chance of finding it the next time I look for it instead of having to happen upon all of the little pieces of advice that are scattered around the internet. Maybe someone else will find it useful, too?
-
In my current use cases, I’m having the R script write a file and output the name of the file to stdout so that the PHP script knows where the file is. This works so much better when I have my R script
write(filename, stdout())
than when I have itprint(filename)
. -
In my call to
exec
, I append. "2>&1"
to the command so that I capture both stderr and stdout. This gives me a better chance of figuring out what is going wrong. -
For the stuff that I am writing, it is much more efficient for me to start by using absolute path names for everything. This way I don’t have to keep track of whether the webserver’s user has the same things in its
PATH
that I have in mine.