加载中…
个人资料
枝叶飞扬
枝叶飞扬
  • 博客等级:
  • 博客积分:0
  • 博客访问:1,922,867
  • 关注人气:217
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
相关博文
推荐博文
谁看过这篇博文
加载中…
正文 字体大小:

The difference between require() and library() in R

(2016-03-29 13:43:22)
标签:

r

require

library

package

installation

分类: 数学
from http://stackoverflow.com/questions/5595512/what-is-the-difference-between-require-and-library
from http://yihui.name/en/2014/07/library-vs-require/

library() loads a package, and will throw an error if the package is not found, the program stops.
require() tries to load a package == try(library()), it will output a warning and continue if the package is not found. In other words, it will not signal the error, the program continues.

Benefic of require():returns a logical value by default. TRUE if the packages is loaded, FALSE if it isn't.

Thus, require() can be used as a condition in if/else statements, especially when you want to distribute your code to our R installation and the required packages might not be installed.

if(require("lme4")){

    print("lme4 is loaded correctly")

} else {

    print("trying to install lme4")

    install.packages("lme4")

    if(require(lme4)){

        print("lme4 installed and loaded")

    } else {

        stop("could not install lme4")

    }

}

or

if (!require('foo')) {

  stop('The package foo was not installed')

 

}


0

阅读 评论 收藏 转载 喜欢 打印举报/Report
  • 评论加载中,请稍候...
发评论

    发评论

    以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

      

    新浪BLOG意见反馈留言板 电话:4000520066 提示音后按1键(按当地市话标准计费) 欢迎批评指正

    新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 会员注册 | 产品答疑

    新浪公司 版权所有