Roman Degtiarev
Roman Degtiarev

Reputation: 35

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.

Upvotes: 2

Views: 1228

Answers (2)

Jacobus
Jacobus

Reputation: 119

I tried littlr, seems amazing, but don't want to work on R v4.0. Rserve seems cool but like you pointed out it seems to be an overkill.

I end up limiting the import to the functions I need. For example:

library(dplyr, include.only = c("select", "mutate","group_by", "summarise", "filter" , "%>%", "row_number", 'left_join', 'rename') )

Upvotes: 0

Dirk is no longer here
Dirk is no longer here

Reputation: 368439

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.

Upvotes: 2

Related Questions