Can't load R libraries using RScript command in PHP
one text
I am trying to connect my R script with my PHP/HTML code. I'm quite well-versed in R but a novice in PHP. My R code requires the use of packages that the PHP command will not load.
Here is the R script:
print(Sys.time())
library(tinytest, lib.loc = c(".../R/R-4.2.1/library","...www\\mypage"))
print("worked")
And here is the sample PHP script:
<?php
echo "Testing";
echo shell_exec ("Rscript /.../www/mypage/my_script.R");
?>
What ends up being printed is as follows:
Testing [1] "xxxx-xx-xx xx:xx:xx EDT"
The PHP and R scripts are saved in the same www location. R is installed on the computer.
I've tried a variety of solutions on both R and PHP:
For R:
- Running .libPaths() on R to identify where the library paths are.
- Adding that library path to the install.packages and library function calls.
- Installing all of my packages to the www location and added that to lib.loc in my library and install.packages calls.
- Adding the dependencies = TRUE on install.packages
- Manually installing each and every dependency on all locations
- Manually calling each library by name
- Ran print(cat(capture.output(file.info("xxx/xxx/xxx")), file = stderr())) to make sure that R has permissions to read the downloaded libraries
- I tried setting the working directory, but PHP would error out
- I used "Sys.getenv('R_LIBS_USER')" to get the current directory and used that to no avail
For PHP:
- I tried using the Rscript command and its full directory path to the file
- I ruled it out being an RScript issue by locating my R.exe and Rscript.exe files
- I tried using the "--default-packages =" command through Rscript (got an error)
- I tried using this commonly posted solution but to no avail
I have tried many solutions but these are the ones I suspect:
- Permissions issues: I am not the administrator on this computer
- Some sort of directory path/library read issue
I'm not sure what else I can do. I could try and look into Apache and RServe, but some permissions issues make it difficult. I've looked into a lot of stackoverflow questions around this topic, but still am unable to solve it. Any ideas?
I am running R version 4.2.1, Windows 10, and PHP version 5.4.16.
Source