0

I am Using R integrated with PHP web application. I am using exec command that runs my R script, each time I run a the script rscript need to load a library which takes 10 seconds to load. making the response of the request time taking. Is there any way using R with web(PHP) to have R session active with library loaded for each user active session. By this user will be using the same R script with different input again and again and I don't want the library to be loaded again and again for each request.

  • not really. every http request is independent of each other. once you've loaded R in response to a request, it'll be UNLOADED when the request finishes. this isn't something you can really "fix" with just php. you need to have an application server, which keeps something running, indepedently of whether the owning user is connected or not. – Marc B May 25 '15 at 20:19
  • Maybe you could have an R process running that listens for files generated by php (with unique process-time name or something) and when found, R produce an output file with similar naming that the php process listens to and reads. – Nightwriter May 25 '15 at 20:54
  • MarcB, @Nightwriter You mean when a user session start for each user start exec("Rscript my_rscript.R &") and make my R script to listen to a request and generate output. – Yeshvant Kumar Bhavnasi May 25 '15 at 21:57
  • Well, I actually meant having an R process running at all time that would always process eg. files in a folder planted by the user session. The R process could then produce the output as an input file which the php process could recognize by its unique name. I am not sure if permission issues makes this impossible though. – Nightwriter May 25 '15 at 22:24
  • Have you considered [opencpu](http://opencpu.org) or [Rserve](https://rforge.net/Rserve/)? The former doesn't necessarily fit what you need (but works well if you can alter your computational model somewhat), the latter perhaps is a little better fit based on your description. – r2evans May 25 '15 at 22:39
  • @r2evans see I have to generate a graph using a library I am taking inputs from from processing the input validating it and send it to the script using rscript. can I do this using Rserve?? Can I run my Script using Rserve where can I find the examples of using Rserve with PHP. – Yeshvant Kumar Bhavnasi May 26 '15 at 00:05
  • I'm more familiar with [opencpu](http://opencpu.org) and it can do what you want, I believe. If you can create a function that takes the input, does all the verification you need, and then produces the output (so all you'd be doing on the console is something like `myfunc(myinput1, myinput2, ...)` and a graph appears), then you would put it in a package (simple), install the package, and then voila, it works. I generally assume the requesting browser must send things in JSON, but there are certainly other methods. Opencpu examples are good. – r2evans May 26 '15 at 00:10
  • I believe Rserve will also do what you want. I just haven't played with it (yet). – r2evans May 26 '15 at 00:10
  • @r2evans can we install a library on opencpu. I could see my required library bioc pathview R library not installed. and I see that there is no good documentation for rserve usage with PHP – Yeshvant Kumar Bhavnasi May 26 '15 at 16:57
  • Easily. If using `opencpu` in development mode (on the *R* console), then all packages normally available to *R* are available via the (single-threaded) instance. For the production system, packages need to be installed in the global library; this is documented in the Server Manual available on the [opencpu download page](https://www.opencpu.org/download.html), currently on page 6. (This is true if installed via `apt-get` or as a `docker` container.) – r2evans May 26 '15 at 17:03

0 Answers0