Is there a way to speed up loading a library in R?

I have rscript that will load ggplot2 in its first line.

Although loading the library does not take much time, since this script can be executed on the command line millions of times, so speed is really important to me.

Is there a way to speed up this boot process?

+8
performance r statistics
source share
3 answers

As a complement to @MikeDunlavey's answer :

Actually, both library and require check if the package is loaded. Here are some timings with microbenchmark I get:

 > microbenchmark (`!` (exists ("qplot")), `!` (existsFunction ('qplot')), require ('ggplot2'), library ('ggplot2'), "package:ggplot2" %in% search ()) ## results reordered with descending median: Unit: microseconds expr min lq median uq max 3 library("ggplot2") 259.720 262.8700 266.3405 271.7285 448.749 1 !existsFunction("qplot") 79.501 81.8770 83.7870 89.2965 114.182 5 require("ggplot2") 12.556 14.3755 15.5125 16.1325 33.526 4 "package:ggplot2" %in% search() 4.315 5.3225 6.0010 6.5475 9.201 2 !exists("qplot") 3.370 4.4250 5.0300 6.2375 12.165 

For comparison, loading for the first time:

 > system.time (library (ggplot2)) User System verstrichen 0.284 0.016 0.300 

(it's seconds!)

In the end, as long as the factor 3 = 10 ยตs between require and "package:ggplot2" %in% search() not needed, I would go with require , otherwise with %in% search () .

+2
source share

Do not restart - keep a constant R session and just send it requests. Something like Rserve can provide this, and for example, FastRWeb uses it very well - with a millsecond round-trip to generate diagrams.

+9
source share

What Dirk said, plus you can use the exists function to conditionally load the library, as in

 if ( ! exists( "some.function.defined.in.the.library" )){ library( the.library ) } 

So, if you put this in a script, you can run the script more than once in the same R session.

+2
source share

All Articles