require() 和 library() 有什么区别? [英] What is the difference between require() and library()?

查看:38
本文介绍了require() 和 library() 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

require()library() 有什么区别?

推荐答案

除了已经给出的好建议,我还要补充一点:

In addition to the good advice already given, I would add this:

最好避免使用 require() 除非您实际上将使用它返回的值,例如在某些错误检查循环中,例如由 thierry 给出.

It is probably best to avoid using require() unless you actually will be using the value it returns e.g in some error checking loop such as given by thierry.

在大多数其他情况下,最好使用 library(),因为如果包不可用,这将在包加载时给出错误消息.如果包不存在,require() 只会失败而不会出错.这是确定是否需要安装软件包的最佳时机(或者可能因为拼写错误而根本不存在).在相关时间及早获得错误反馈将避免在尝试使用库例程时追踪后面的代码失败的原因可能带来的麻烦

In most other cases it is better to use library(), because this will give an error message at package loading time if the package is not available. require() will just fail without an error if the package is not there. This is the best time to find out if the package needs to be installed (or perhaps doesn't even exist because it it spelled wrong). Getting error feedback early and at the relevant time will avoid possible headaches with tracking down why later code fails when it attempts to use library routines

这篇关于require() 和 library() 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆