Loading and Listing of Packagesから。
library(package) and require(package) both load the package with name package. require is designed for use inside other functions; it returns FALSE and gives a warning (rather than an error as library() does by default) if the package does not exist.
面白そうなので、試してみる。
まずはRでパッケージを作る準備。package.skeletonは便利
f <- function(x,y) x+y
package.skeleton(list=c("f"), name="testpkg")
f.Rの先頭で存在しないパッケージを読み込む
library(dummypkg) # or require(dummypkg)
f <-
function(x,y) x+y
libraryのほうはエラーになってインストール出来ないが、
$R CMD INSTALL testpkg
* installing to library ‘/Library/Frameworks/R.framework/Resources/library’
* installing *source* package ‘testpkg’ ...
** R
** preparing package for lazy loading
Error in library(dummypkg) :
'dummypkg' という名前のパッケージが見当たりません
ERROR: lazy loading failed for package ‘testpkg’
* removing ‘/Library/Frameworks/R.framework/Resources/library/testpkg’
requireではwarningが出るが先に進む。
$ R CMD INSTALL testpkg
* installing to library ‘/Library/Frameworks/R.framework/Resources/library’
* installing *source* package ‘testpkg’ ...
** R
** preparing package for lazy loading
Loading required package: dummypkg
library(package, lib.loc = lib.loc, character.only = TRUE,\
logical.return = TRUE, 中で警告がありました:
'dummypkg' という名前のパッケージが見当たりません