Search code examples
rscalarscriptrserve

Avoid loading libraries on multiple run of R script


I need to run (several times) my R script (script.R), which basically looks like this:

library(myLib)
cmd = commandArgs(TRUE)
args=myLib::parse.cmd(cmd)
myLib::exec(args)

myLib is my own package, which load some dependencies (car, minpack.lm, plyr, ggplot2). The time required for loading libraries is comparable with the time of myLib::exec, so I'm looking for a method which helps me not to load them every time I call Rscript script.R

I know about Rserve, but it looks like a little bit overkill, though it could do exactly what I need. Is there any other solutions?

P.S: I call script.R from JVM using Scala.


Solution

  • Briefly:

    • on startup you need to load your libraries
    • if you call repeatedly and start repeatedly you repeatedly load the libraries
    • you already mentioned a stateful solution (Rserve) which allows you start it once but connect and eval multiple times

    so I think you answered your question.

    Otherwise, I enjoy littler and have shown how it starts faster than either R or Rscript -- but the fastest approach is simply not to restart.