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

查看:97
本文介绍了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天全站免登陆