Calling an R-script from PHP: pitfall with loading R-packages

We continue developing and soon will make the first tool from the promised portfolio optimization toolset publicly available. And so far a small technical report. We implement the core business logic in R and wrap-up it with PHP. Calling Rscript from PHP via exec("Rscript ...")  seems to be easy ... as far as you don't use any libraries. And if you do, you will get an error message like Error in library(RMySQL) : there is no package called 'RMySQL'. Fortunately, there is a solution. However, note that a loaded package may depend on other packages. For instance, RMySQL depends on DBI. Thus you should explicitly resolve this dependency in your R-code like this:

library(DBI, lib.loc = c("/home/<yourUsername>/R/x86_64-pc-linux-gnu-library/<yourRVersion>", "/usr/local/lib/R/site-library", "/usr/lib/R/library"))
library(RMySQL, lib.loc = c("/home/<yourUsername>/R/x86_64-pc-linux-gnu-library/<yourRVersion>", "/usr/local/lib/R/site-library", "/usr/lib/R/library"))

Leave a Reply

Your email address will not be published. Required fields are marked *