3

It usually takes some time to load an R package.

R> system.time(library(ggplot2))
   user  system elapsed 
  0.686   0.084   0.980 
R> system.time(ggplot2::qplot) # run in a different R session
   user  system elapsed 
  0.612   0.088   0.906 

If I just want to use a few functions in a package, it would be better to reduce the package load time. For example, if I just want to use qplot() and ggsave(), is there a way to speed up the package load time of ggplot2?

More generally, is there a standard method to reduce the pacakge load time for any package. Thanks.

user1424739
  • 11,937
  • 17
  • 63
  • 152
  • 2
    I don't know how much it helps, but you can use the functions directly without loading the package via e.g. `ggplot2::qplot()`, `ggplot2::ggsave()` – Ben Bolker May 04 '20 at 01:04
  • 2
    The package still loads. See the update. – user1424739 May 04 '20 at 01:14
  • oh well ....... – Ben Bolker May 04 '20 at 01:20
  • 1
    identical (but not answered): https://stackoverflow.com/questions/42933479/how-to-speed-up-the-package-library-loading-time-in-r – Ben Bolker May 04 '20 at 01:20
  • 1
    can you say more about the context/why it's important to reduce the load time? It's <1 second per R session. Are you going to be running lots and lots of sessions? Answers [here](https://stackoverflow.com/questions/41569997/is-it-possible-to-run-r-as-a-daemon) about running a shared R session ... – Ben Bolker May 04 '20 at 01:28
  • I use littler. So it is not per R session, it is per littler call, which can be quite significant if I call littler on many scripts or on a script many times. Running R as a deamon is an overkill solution. I don't think it is relevant to my usage. – user1424739 May 04 '20 at 01:45
  • 4
    It is an interesting question, whether one could avoid loading the package namespace while using only the functions from it you need; given how I understand package namespaces to work, I'm tempted to say there is no way to avoid it, but I'm not 100% confident in that assessment. I might rethink your workflow honestly; I don't know much about littler, but it sounds like a pretty inefficient way to do analysis. – duckmayr May 04 '20 at 01:47
  • 4
    OK, looking at `littler`, it's like a slightly different version of `Rscript`. So, this is definitely a workflow issue. You should be able to set up the scripts for your project in such a way that you're not loading the packages you need many times. For example, I recently did graphics work for a book project that used `ggplot2`. I had a script, executed via `Rscript`, that recreated all figures for the book any time needed. It loaded and attached the `ggplot2` package **once**, at the beginning of the script, then called (`source()`d) the individual chapter scripts. – duckmayr May 04 '20 at 01:53

0 Answers0