R语言中library()和require()的区别

来源:互联网 发布:智能电视网络dns设置 编辑:程序博客网 时间:2024/05/28 23:10

While I was sitting in a conference room at UseR! 2014, I started counting the number of times that require() was used in the presentations, and would rant about it after I counted to ten. With drums rolling, David won this little award (sorry, I did not really mean this to you).

After I tweeted about it, some useRs seemed to be unhappy and asked me why. Both require() and library() can load (strictly speaking, attach) an R package. Why should not one use require()? The answer is pretty simple. If you take a look at the source code of require (use the source, Luke, as Martin Mächler mentioned in his invited talk), you will see that require() basically means “try to load the package using library() and return a logical value indicating the success or failure”. In other words, library() loads a package, and require() tries to load a package. So when you want to load a package, do you load a package or try to load a package? It should be crystal clear.

One bad consequence of require() is that if you require('foo') in the beginning of an R script, and use a function bar() in the foo package on line 175, R will throw an error object “bar” not found if foo was not installed. That is too late and sometimes difficult for other people to understand if they use your script but are not familiar with the foopackage – they may ask, what is the bar object, and where is it from? When your code is going to fail, fail loudly, early, and with a relevant error message. require() does not signal an error, and library() does.

Sometimes you do need require() to use a package conditionally (e.g. the sun is not going to explode without this package), in which case you may use an if statement, e.g.

if (require('foo')) {  awesome_foo_function()} else {  warning('You missed an awesome function')}

That should be what require() was designed for, but it is common to see R code like this as well:

if (!require('foo')) {  stop('The package foo was not installed')}

Sigh.

  • library('foo') stops when foo was not installed
  • require() is basically try(library())

Then if (!require('foo')) stop() is basically “if you failed to try to load this package, please fail”. I do not quite understand why it is worth the circle, except when one wants a different error message with the one from library(), otherwise one can simply load and fail.

There is one legitimate reason to use require(), though, and that is, “require is a verb and library is a noun!” I completely agree. require should have been a very nice name to choose for the purpose of loading a package, but unfortunately… you know.

If you take a look at the StackOverflow question on this, you will see a comment on “package vs library” was up-voted a lot of times. It used to make a lot of sense to me, but now I do not care as much as I did. There have been useRs (including me up to a certain point) desperately explaining the difference between the two terms package and library, but somehow I think R’s definition of a library is indeed unusual, and the function library()makes the situation worse. Now I’m totally fine if anyone calls my packages “libraries”, because I know what you mean.

Karthik Ram suggested this GIF to express “Ah a new library, but require? Noooooo“:

Since you have read the source code, Luke, you may have found that you can abuse require() a bit, for example:

> (require(c('MASS', 'nnet')))c("Loading required package: c", "Loading required package: MASS",  "Loading required package: nnet")Failed with error:  ‘'package' must be of length 1’In addition: Warning message:In if (!loaded) { :  the condition has length > 1 and only the first element will be used[1] FALSE> (require(c('MASS', 'nnet'), character.only = TRUE))c("Loading required package: MASS", "Loading required package: nnet")Failed with error:  ‘'package' must be of length 1’In addition: Warning message:In if (!loaded) { :  the condition has length > 1 and only the first element will be used[1] FALSE> library(c('MASS', 'nnet'), character.only = TRUE)Error in library(c("MASS", "nnet"), character.only = TRUE) :   'package' must be of length 1

So require() failed not because MASS and nnet did not exist, but because of a different error. As long as there is an error (no matter what it is), require() returns FALSE.

One thing off-topic while I’m talking about these two functions: the argument character.only = FALSE for library() and require() is a design mistake in my eyes. It seems the original author(s) wanted to be lazy to avoid typing the quotes around the package name, so library(foo) works like library("foo"). Once you show people they can be lazy, you can never pull them back. Apparently, the editors of JSS (Journal of Statistical Software) have been trying to promote the form library("foo")and discourage library(foo), but I do not think it makes much sense now or it will change anything. If it were in the 90’s, I’d wholeheartedly support it. It is simply way too late now. Yes, two extra quotation marks will kill many kittens on this planet. If you are familiar with *nix commands, this idea is not new – just think about tar -z -x -ftar -zxf, and tar zxf.

One last mildly annoying issue with require() is that it is noisy by default, because of the default quietly = FALSE, e.g.

> require('nnet')Loading required package: nnet> require('MASS', quietly = TRUE)

So when I tell you to load a package, you tell me you are loading a package, as if you had heard me. Oh thank you!






转载自:https://yihui.name/en/2014/07/library-vs-require/


相关链接:http://www.cnblogs.com/todoit/archive/2012/10/24/2736514.html


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 一岁发烧39.8度怎么办 反复发烧39度4天怎么办 大人发烧到39度怎么办 小孩高烧39度多怎么办 孩子嗓子发炎发烧怎么办吃什么药 猫咪吃了点桃子怎么办 猫吃了牛油果怎么办 苹果手机死机开不了机怎么办 新生儿出生第一天没奶水怎么办 第一天断奶奶水一直流出怎么办 3岁宝宝不长个子怎么办 5个月宝宝不长个怎么办 孩子比同龄人矮很多怎么办 孕妇做春梦宫缩怎么办 减肥掉头发很厉害怎么办 孕妇吃了金枪鱼罐头怎么办 怀孕吃了烂水果怎么办 快递水果压坏了怎么办 谈对象被骗了钱怎么办 如果遇到半夜坏人敲门怎么办 小孩晚上不进房间睡觉怎么办 房子照不到阳光潮湿怎么办 4个月婴儿不拉屎怎么办 小孩六个月奶不够吃怎么办 到晚上就没奶水怎么办 20多天奶水不够怎么办 四个月奶不够吃怎么办 7个月宝宝不肯吃东西怎么办 6个月宝宝不肯吃东西怎么办 十一个月宝宝不肯吃东西怎么办 九个月宝宝不肯吃辅食怎么办 八个月宝宝不肯吃辅食怎么办 七个月宝宝不肯吃辅食怎么办 7个月宝不吃辅食怎么办 在月子里奶不够怎么办 8月宝宝不吃辅食怎么办 8个月不吃辅食怎么办 宝宝不吃药怎么办一岁 奶水不足宝宝又不吃奶粉怎么办 一岁婴儿突然不吃饭怎么办 一岁宝宝突然不吃饭怎么办