ggplot2
最初の行でロードする Rscript があります。
ライブラリのロードにはそれほど時間はかかりませんが、このスクリプトはコマンドラインで何百万回も実行される可能性があるため、速度は私にとって非常に重要です。
この読み込みプロセスを高速化する方法はありますか?
ggplot2
最初の行でロードする Rscript があります。
ライブラリのロードにはそれほど時間はかかりませんが、このスクリプトはコマンドラインで何百万回も実行される可能性があるため、速度は私にとって非常に重要です。
この読み込みプロセスを高速化する方法はありますか?
@MikeDunlaveyの答えへの追加として:
実際には、両方library
とrequire
パッケージがすでにロードされているかどうかを確認してください。これが私が得るいくつかのタイミングmicrobenchmark
です:
> 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
比較のために、初めてロードする:
> system.time (library (ggplot2))
User System verstrichen
0.284 0.016 0.300
(これらは秒です!)
結局、係数3 =10μsの間require
で"package:ggplot2" %in% search()
必要がない限り、私は、を使用します。require
それ以外の場合は、を使用しません%in% search ()
。
Dirkが言ったことに加えて、次のように、exists
関数を使用して条件付きでライブラリをロードできます
if ( ! exists( "some.function.defined.in.the.library" )){
library( the.library )
}
したがって、それをスクリプトに入れると、同じ R セッションでスクリプトを複数回実行できます。