What is the difference between require()
and library()
?
相关问题
- R - Quantstart: Testing Strategy on Multiple Equit
- Using predict with svyglm
- Reshape matrix by rows
- Extract P-Values from Dunnett Test into a Table by
- split data frame into two by column value [duplica
相关文章
- How to convert summary output to a data frame?
- How to plot smoother curves in R
- Paste all possible diagonals of an n*n matrix or d
- ess-rdired: I get this error “no ESS process is as
- How to use doMC under Windows or alternative paral
- dyLimit for limited time in Dygraphs
- Saving state of Shiny app to be restored later
- How to insert pictures into each individual bar in
and you will see:
require()
loads all additional packagesThere's not much of one in everyday work.
However, according to the documentation for both functions (accessed by putting a
?
before the function name and hitting enter),require
is used inside functions, as it outputs a warning and continues if the package is not found, whereaslibrary
will throw an error.In addition to the good advice already given, I would add this:
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.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 routinesMy initial theory about the difference was that
library
loads the packages whether it is already loaded or not, i.e. it might reload an already loaded package, whilerequire
just checks that it is loaded, or loads it if it isn't (thus the use in functions that rely on a certain package). The documentation refutes this, however, and explicitly states that neither function will reload an already loaded package.Always use
library
. Never1 userequire
.(1 Almost never. Maybe.)
In a nutshell, this is because, when using
require
, your code might yield different, erroneous results, without signalling an error. This is rare but not hypothetical! Consider this code, which yields different results depending on whether {dplyr} can be loaded:This can lead to subtly wrong results. Using
library
instead ofrequire
throws an error here, signalling clearly that something is wrong. This is good.It also makes debugging all other failures more difficult: If you
require
a package at the start of your script and use its exports in line 500, you’ll get an error message “object ‘foo’ not found” in line 500, rather than an error “there is no package called ‘bla’”.The only acceptable use case of
require
is when its return value is immediately checked, as some of the other answers show. This is a fairly common pattern but even in these cases it is better (and recommended, see below) to instead separate the existence check and the loading of the package.More technically,
require
actually callslibrary
internally (if the package wasn’t already attached —require
thus performs a redundant check, becauselibrary
also checks whether the package was already loaded). Here’s a simplified implementation ofrequire
to illustrate what it does:Experienced R developers agree:
Yihui Xie, author of {knitr}, {bookdown} and many other packages says:
Hadley Wickham, author of more popular R packages than anybody else, says