Reputation: 2049
I am trying to execute the following simple R script via PHP but I am always getting exit code 1 if I put library()
function call.
library('optparse')
quit()
The PHP script 'test.php' is given as:
<?php
$calc = exec("Rscript test.r", $R_output, $R_exitcode);
print $calc_routine;
The script produces exit code 0 when I remove library()
function call, so it's sure that R script is located correctly.
Upvotes: 2
Views: 908
Reputation: 42715
There is an environment variable called R_LIBS_USER
that tells R where to look for libraries. By default, it is set to a subdirectory in your home directory. Of course when called from a web server, you will be not be running it – the home directory will be different.
First you'll need to determine what your library directory is. From R, call .libPaths()
to get it.
Then, you can either tell PHP about the updated value:
<?php
setenv("R_LIBS_USER", "/path/to/your/libraries");
$calc = exec("Rscript test.r", $R_output, $R_exitcode);
Or, in your R code, use .libPaths with a parameter to set the library path to the new value:
.libPaths('/path/to/your/libraries')
library('optparse')
quit()
Upvotes: 1