Drkcore

24 06 2010 R Tweet

Rのrequireとlibrary

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' という名前のパッケージが見当たりません  

ProductName Rの基礎とプログラミング技法
U.リゲス
シュプリンガー・ジャパン(株) / ¥ 3,675 ()
在庫あり。

About

  • もう5年目(wishlistありマス♡)
  • 最近はPythonとDeepLearning
  • 日本酒自粛中
  • ドラムンベースからミニマルまで
  • ポケモンGOゆるめ

Tag

Python Deep Learning javascript chemoinformatics Emacs sake and more...

Ad

© kzfm 2003-2021